请问是不是定义构造函数的时候参数不能用char n[ ] ,还是那个李四哪里不对?

代码如下:
class Student
{
public:
Student();
Student(char n[]);
void print();
void setName();
private:
char name[50];
};

#include<iostream.h>
#include<string.h>
#include"student.h"
void Student::print()
{
cout << "hello, this is "<<name<<"'s first try!" << endl;
}
void Student::setName()
{
char n[50];
cout <<"please enter your name :"<<endl;
cin>>n;
strcpy(name,n);
}
Student::Student(){strcpy(name,"张三");}
Student::Student(char n[]){strcpy(name,n);}

#include<iostream.h>
#include<stdlib.h>
#include"student.h"
int main()
{
Student student,student1("李四");
student.setName();
student1("李四").setName();
student.print();
student1("李四").print();
return 0;
}

错误提示 student1("李四").setName(); student1("李四").print(); 这两行term does not evaluate to a function left of '.setName' must have class/struct/union type 是不是定义构造函数的时候参数不能用char n[ ] 还是那个李四哪里不对 我晕掉了 最近刚开始学C++ 求大神指点

紫衣仙女
浏览 140回答 1
1回答

繁花如伊

这个不行的,直接student1.setName();就行了!构造函数只会被执行一次!
打开App,查看更多内容
随时随地看视频慕课网APP