為什么在慕課中總報(bào)錯(cuò),在vs2010就沒問題?
#include <iostream>
#include <string>
using namespace std;
/**
? * 定義類:Student
? * 數(shù)據(jù)成員:m_strName
? * 數(shù)據(jù)成員的封裝函數(shù):setName()、getName()
? */
class Student
{
public:
? ? // 定義數(shù)據(jù)成員封裝函數(shù)setName()
? ? void setName(string _name)
? ? {
? ? m_strName=_name;
? ? }
? ??
? ? // 定義數(shù)據(jù)成員封裝函數(shù)getName()
? ? string getName()
? ? {
? ? return m_strName; ? ?
? ? }
? ??
? ??
//定義Student類私有數(shù)據(jù)成員m_strName
private:
? ? ? string m_strName;
};
int main()
{
? ? // 使用new關(guān)鍵字,實(shí)例化對(duì)象
Student *str =new Student;
? ? // 設(shè)置對(duì)象的數(shù)據(jù)成員
str->setName("慕課網(wǎng)");
? ? // 使用cout打印對(duì)象str的數(shù)據(jù)成員
? ? cout<<str->getName()<<endl;
? ? // 將對(duì)象str的內(nèi)存釋放,并將其置空
delete str;
str=NULL;
return 0;
}
2019-03-27
感覺還是用2017版本的吧!2010很老的版本了
2018-11-25
private?的冒號(hào)打錯(cuò)了
2018-11-11
你用new關(guān)鍵字實(shí)例化對(duì)象的時(shí)候應(yīng)該是Student *str =new Student();你少了一個(gè)括號(hào)。將對(duì)象置空是? str = nullptr;,你可以試一下