为了账号安全,请及时绑定邮箱和手机立即绑定

拷贝构造函数

#include<string>

using namespace std;

#include<iostream>

class Teacher

{

private:

string m_strName;

int m_IAge;

const int m_iMax;

public:

void setName(string name);

string getName();

void setAge(int age);

int getAge();

int getMax();

Teacher(const Teacher&tea)

{

cout << "Teacher(const Teacher&tea)" << endl;

}//拷贝构造函数

Teacher(string name = "Jim", int age = 35,int m=100);

};

Teacher::Teacher(string name, int age,int m):m_strName(name), m_IAge(age),m_iMax(m)

{

cout << "Teacher(string name, int age)" << endl;

}



int Teacher::getMax()

{

return m_iMax;

}

void Teacher::setName(string name)

{

m_strName = name;

}

string Teacher::getName()

{

return m_strName;

}

void Teacher::setAge(int age)

{

m_IAge = age;

}

int Teacher::getAge()

{

return m_IAge;

}

void main()

{

Teacher t1;

//t1.setAge(25);

//t1.setName("jane");

cout << t1.getAge() << " " << t1.getName() <<" "<<t1.getMax()<< endl;

Teacher t2("lily", 20,150);

//t2.setAge(25);

//t2.setName("jane");

cout << t2.getAge() << " " << t2.getName() << " "<<t2.getMax()<<endl;

Teacher t3;

Teacher t4 = t3;

Teacher t5(t3);

system("pause");

}

在运行时提示错误:Teacher::m_iMAx 必须初始化常量限定类型的对象

哪位大神可以解答一下呀,万分感谢


正在回答

3 回答

const int m_iMax;把const去掉,还有你的初始化列表要放到类里面,这句:

Teacher::Teacher(string name, int age,int m):m_strName(name), m_IAge(age),m_iMax(m)

{

cout << "Teacher(string name, int age)" << endl;

}


0 回复 有任何疑惑可以回复我~
#1

慕粉160702859 提问者

非常感谢!
2016-10-29 回复 有任何疑惑可以回复我~
#2

weibo_胡睿承_03901184

为什么要把初始化列表放入到类内?
2017-01-11 回复 有任何疑惑可以回复我~
#3

面具VS口罩 回复 weibo_胡睿承_03901184

不放到类里边就需要类外份文件定义
2017-03-21 回复 有任何疑惑可以回复我~

如果带着这个常量怎么定义拷贝函数呢

0 回复 有任何疑惑可以回复我~

请问为什么要把m_iMax的const去掉啊?

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之封装篇(上)
  • 参与学习       103381    人
  • 解答问题       732    个

封装--面向对象的基石,本教程力求帮助小伙伴们即学即会

进入课程

拷贝构造函数

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号