首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于构造函数初始化有关问题

2012-03-19 
关于构造函数初始化问题classteacher{charclassname[5][100]//教的班级名最多都五个班charbixiuke[5][100

关于构造函数初始化问题
class   teacher
{
char   classname[5][100];     //教的班级名     最多都五个班
char   bixiuke[5][100];     //教的必修课名
char   xianxiuke[5][100];   //教的选修课名     最多教五门课
public:
teacher();
};

teacher::teacher()
{
classname[5][100]={ "norecord ", "norecord ", "norecord ", "norecord ", "norecord "};

bixiuke[5][100]={ "norecord ", "norecord ", "norecord ", "norecord ", "norecord "};

xianxiuke[5][100]={ "norecord ", "norecord ", "norecord ", "norecord ", "norecord "};
}


很奇怪,编译时提示:
D:\Program   Files\Microsoft   Visual   Studio\MyProjects\ex1\ex1.cpp(47)   :   error   C2059:   syntax   error   :   '{ '
D:\Program   Files\Microsoft   Visual   Studio\MyProjects\ex1\ex1.cpp(47)   :   error   C2143:   syntax   error   :   missing   '; '   before   '{ '
D:\Program   Files\Microsoft   Visual   Studio\MyProjects\ex1\ex1.cpp(47)   :   error   C2143:   syntax   error   :   missing   '; '   before   '} '
D:\Program   Files\Microsoft   Visual   Studio\MyProjects\ex1\ex1.cpp(49)   :   error   C2059:   syntax   error   :   '{ '
D:\Program   Files\Microsoft   Visual   Studio\MyProjects\ex1\ex1.cpp(49)   :   error   C2143:   syntax   error   :   missing   '; '   before   '{ '
D:\Program   Files\Microsoft   Visual   Studio\MyProjects\ex1\ex1.cpp(49)   :   error   C2143:   syntax   error   :   missing   '; '   before   '} '
D:\Program   Files\Microsoft   Visual   Studio\MyProjects\ex1\ex1.cpp(51)   :   error   C2059:   syntax   error   :   '{ '
D:\Program   Files\Microsoft   Visual   Studio\MyProjects\ex1\ex1.cpp(51)   :   error   C2143:   syntax   error   :   missing   '; '   before   '{ '
D:\Program   Files\Microsoft   Visual   Studio\MyProjects\ex1\ex1.cpp(51)   :   error   C2143:   syntax   error   :   missing   '; '   before   '} '

请问这是怎么回事啊?我单独把这个类注释掉:
在main函数里:
void   mail()
{
char   classname[5][100]={ "norecord ", "norecord ", "norecord ", "norecord ", "norecord "};
}
编译时是没有问题的,请问为什么在类内就不行呢?



[解决办法]
使用strcpy进行赋值

strcpy(classname[0], "norecord ");
strcpy(classname[1], "norecord ");
strcpy(classname[2], "norecord ");
strcpy(classname[3], "norecord ");
strcpy(classname[4], "norecord ");
[解决办法]
char classname[5][100]={ "norecord ", "norecord ", "norecord ", "norecord ", "norecord "};
编译时是没有问题的,请问为什么在类内就不行呢?
---------------
char classname[5][100];
classname[5][100] ={ "norecord ", "norecord ", "norecord ", "norecord ", "norecord "};
你看看问题是不是来了。
BTW:写成classname={ "norecord ", "norecord ", "norecord ", "norecord ", "norecord "};是普通错误,写成classname[5][100] ={ "norecord ", "norecord ", "norecord ", "norecord ", "norecord "};是高级错误


[解决办法]
数组一旦定义了就最好初始化,不初始化话只能用修改数组的内容,数组的地址和容量不能修改.
teacher::teacher()
{
//这里classname[5][100]是一个数组的元素,但他实际不存在.非法
classname[5][100]={ "norecord ", "norecord ", "norecord ", "norecord ", "norecord "};

bixiuke[5][100]={ "norecord ", "norecord ", "norecord ", "norecord ", "norecord "};

xianxiuke[5][100]={ "norecord ", "norecord ", "norecord ", "norecord ", "norecord "};
}
[解决办法]
C、C++就这么规定的:不可以。不需要理由。

热点排行