我想给指针数组赋值
#include<iostream>using namespace std;
class CCourse
{
string name;
int creditHour;
public:
void setname(string x)
{name = x;}
string getname()
{return name;}
void setcreditHour(int x)
{creditHour = x;}
int getcreditHour()
{return creditHour;}
};
class CStudent
{
public:
CStudent(int courseNumber,string name,int birthmonth,int birthday)
{
this->courseNumber = courseNumber<=10?courseNumber:10;
this->name = name;
this->birthday = birthday;
this->birthmonth = birthmonth;
}
void set(int x,string coursename,int credit) 《-----------------------------------这里出的问题
{
courseList->setname(coursename);
courseList->setcreditHour(credit);
}
private:
CCourse* courseList;
int courseNumber;
string name;
int birthmonth;
int birthday;
};
int main()
{
CStudent p(4, "张三", 1, 1);
p.set(0, "高数", 4); 《--------------------------------------这里出的问题
p.set(1, "c++", 4);
p.set(2, "线性代数", 3);
p.set(3, "大学英语", 3);
} 只要在构造函数里初始化一下指针数组就行了
for (int i = 0; i <= 9; i++)
{
courseList = new CCourse();
}
页:
[1]