2014-09-03 1 views
0

나는 학생 개체 배열이 코스 안에 있습니다. 학생 이름 []에 할당 할 배열 크기를 어떻게 초기화합니까? 내가 포인터 또는 배열을 사용해야합니까?클래스 개체 내부에 배열을 할당하는 방법

#include <iostream> 
#include "student.h" 
#include "course.h" 

int main(int argc, char** argv) { 

    Student student[4]; 
    Course computerClass(student); 
    return 0; 
} 

#ifndef COURSE_H 
#define COURSE_H 
#include "student.h" 
class Course 
{ 
    private: 
    Student name[]; 
    public: 
     Course(); 
     Course(Student []); 

}; 

#endif 

#include "course.h" 
#include <iostream> 
using namespace std; 
Course::Course() 
{ 
} 

Course::Course(Student []){ 



} 

답변

1
당신은 컴파일시에 배열의 크기를 알고있을 때 당신이하지 않는 경우에만, std::vector을 사용하여 배열을 사용할 수 있습니다

:

#include <iostream> 
#include "student.h" 
#include "course.h" 


int main(int argc, char** argv) { 

    Students students(4); 
    Course computerClass(students); 
    return 0; 
} 

#ifndef COURSE_H 
#define COURSE_H 
#include "student.h" 

typedef std::vector<Student> Students; 

class Course 
{ 
    private: 
     Students names; 
    public: 
     Course(); 
     Course(const Students &students); 

}; 

#endif 

#include "course.h" 
#include <iostream> 
using namespace std; 
Course::Course() 
{ 
} 

Course::Course(const Students &students) : names(students) 
{ 
} 
+0

감사합니다,하지만 당신은 생성자의 매개 변수에 const를 사용합니까 왜 그냥 궁금? – Vanishadow

관련 문제