實例化類時,采用無參的構(gòu)造函數(shù)要出錯是怎么回事?。?/h1>
#include?<iostream>
#include?<string>
using?namespace?std;
class?Teacher
{
public:
Teacher()
{
m_strName?=?"Lily";
cout?<<"?Teacher()"?<<?endl;
}
Teacher(int?_age?=?60)
{
m_iAge?=?_age;
cout?<<"?Teacher(int?_age?=?60)"?<<?endl;
}
void?setName(string?_name)
{
m_strName?=?_name;
}
string?getName()
{
return?m_strName;
}
void?setAge(int?_age)
{
m_iAge?=?_age;
}
int?getAge()
{
return?m_iAge;
}
private?:
string?m_strName;
int?m_iAge;
};
int?main(void)
{
Teacher?t1;
Teacher?t2(50);
//cout?<<?droL.getName()?<<?endl;
system("pause");
}

#include?<iostream> #include?<string> using?namespace?std; class?Teacher { public: Teacher() { m_strName?=?"Lily"; cout?<<"?Teacher()"?<<?endl; } Teacher(int?_age?=?60) { m_iAge?=?_age; cout?<<"?Teacher(int?_age?=?60)"?<<?endl; } void?setName(string?_name) { m_strName?=?_name; } string?getName() { return?m_strName; } void?setAge(int?_age) { m_iAge?=?_age; } int?getAge() { return?m_iAge; } private?: string?m_strName; int?m_iAge; }; int?main(void) { Teacher?t1; Teacher?t2(50); //cout?<<?droL.getName()?<<?endl; system("pause"); }
2017-07-12
t1后面加個()就可以了
2016-11-02
同樣的代碼現(xiàn)在又不報錯了,奇怪