請(qǐng)問(wèn)這樣為什么不行
#include <iostream>
#include <string>
using namespace std;
/**
?* 定義類(lèi):Student
?* 數(shù)據(jù)成員:m_strName
?* 無(wú)參構(gòu)造函數(shù):Student()
?* 有參構(gòu)造函數(shù):Student(string _name)
?* 拷貝構(gòu)造函數(shù):Student(const Student& stu)
?* 析構(gòu)函數(shù):~Student()
?* 數(shù)據(jù)成員函數(shù):setName(string _name)、getName()
?*/
class Student
{
public:
??? Student()
??? {
?? ?
??? }
??? Student(string_name)
??? {
??????? m_strName = _name;
??? };
??? Student(const Student& stu){};
??? ~Student(){};
??? void setName(string);
??? string getName();
private:
??? String m_strName;
};
void setName(string _name)
{
??? m_strName = _name;
}
String getName()
{
??? return m_strName;
}
int main(void)
{
??? // 通過(guò)new方式實(shí)例化對(duì)象*stu
??? Student *stu = new Student;
??? // 更改對(duì)象的數(shù)據(jù)成員為“慕課網(wǎng)”
?? ?stu->setName("慕課網(wǎng)");
??? // 打印對(duì)象的數(shù)據(jù)成員
?? ?cout << stu->getName() << endl;
?? ?delete stu;
?? ?stu = NULL;
?? ?return 0;
}
2021-02-04
你需要注明這個(gè)類(lèi)外定義的函數(shù)是屬于Student這個(gè)類(lèi)的。也就是說(shuō)在類(lèi)外去實(shí)現(xiàn)函數(shù)內(nèi)容的時(shí)候函數(shù)標(biāo)題應(yīng)該是像這樣:void Student::setName(string _name){ //具體內(nèi)容}?