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

形参赋初始值的问题

class Student
{
string m_strName;
public:
Student();
Student(string _name);                  //Student(string _name='Leo')为什么编译不通过呢?
Student(const Student & stu);
~Student();
void SetName(string _name);            //void SetName(string _name='Leo')同上
string GetName();
};
Student::Student()
{
    m_strName="";
    cout<<"Student()"<<endl;
}
Student::Student(string _name)
{
    m_strName=_name;
    cout<<"Student(string _name)"<<endl;
}
Student::Student(const Student& stu)
{
    cout<<"Student(const Student & stu)"<<endl;
}
Student::~Student()
{
    cout<<"~Student"<<endl;
}
void Student::SetName(string _name)
{
    m_strName=_name;
    cout<<"SetName(_name)"<<endl;
}
string Student::GetName()
{
    return m_strName;
    cout<<"GetName()"<<endl;
}
int main(void)
{
    // 通过new方式实例化对象*stu
    Student *stu = new Student();
    // 更改对象的数据成员为“慕课网”
	stu->SetName("慕课网");
    // 打印对象的数据成员
	cout<<stu->GetName()<<endl;
	return 0;
}

如上注释所示,为什么在申明函数的时候给形参赋值通不过呢?我记得前面的课程里好像有在声明函数的时候赋值的

希望前辈指点,谢谢


正在回答

2 回答

Student(string _name='Leo')这样就2个默认构造函数了~要产生二义性的

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

字符串是双引号不是单引号

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

口畏喂马劈劈柴 提问者

改成双引号也编译不通过啊,怎么一回事呢这
2015-05-26 回复 有任何疑惑可以回复我~

举报

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

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

进入课程

形参赋初始值的问题

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

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

帮助反馈 APP下载

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

公众号

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