已采纳回答 / 东31
你把m_dPi在类中定义成常量, 就意味在只能在构造函数中使用初始化列表的方式来初始化, 可是你的复制构造函数却没有用初始化列表的方式来初始化m_dPi的值, 所以编译出错。所以这也能解释为什么你把m_dPi定义成变量编译器就可以通过了。你也可以把复制构造函数这么修改:Teacher::Teacher(const Teacher &tea) :m_dPi(8){ cout <<"Teacher::Teacher(const Teacher &tea)"<<en...
2016-11-29
class Student{
public:
Student(){};
Student(string _name){m_strName = _name;};
Student(const Student& stu){m_strName = stu.m_strName;};
~Student(){};
void setName(string _name){ m_strName =_name;};
string getName(){return m_strName;};
private:
string m_strName;
};
public:
Student(){};
Student(string _name){m_strName = _name;};
Student(const Student& stu){m_strName = stu.m_strName;};
~Student(){};
void setName(string _name){ m_strName =_name;};
string getName(){return m_strName;};
private:
string m_strName;
};
最新回答 / wallEVA96
肯定不行的。 加上括号编译器 会认为是定义了一个返回类的 函数, 无法编译通过,所以无参数就不加括号就好了。视频中应该是 用new 有加括号,没影响。
2016-11-24
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
char*m_strName;
int m_iAge;
};
int main()
{
Student stu;
stu.m_strName = "慕课网";
stu.m_iAge = 2;
cout << stu.m_strName << " " << stu.m_iAge<< endl;
system("pause");
return 0;
}
#include <string>
using namespace std;
class Student
{
public:
char*m_strName;
int m_iAge;
};
int main()
{
Student stu;
stu.m_strName = "慕课网";
stu.m_iAge = 2;
cout << stu.m_strName << " " << stu.m_iAge<< endl;
system("pause");
return 0;
}