大家看看这段代码哪里出了问题
// dd.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
this->salary = 300;
}
int getsalary()
{
return this->salary;
}
private:
int salary;
};
int main(int argc, char* argv[])
{
Person *p;
cout << "工资 = " << p->getsalary() << endl;
/*
Person p;
cout << "工资 = " << p.getsalary() << endl;
*/
}
[解决办法]
#include "stdafx.h
#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
this->salary = 300;
}
int getsalary()
{
return this->salary;
}
private:
int salary;
};
int main(int argc, char* argv[])
{
Person *p=new Person();//需要给指针p初始化,不然就是野指针,光定义Person类型的指针是不会构造Person类的。
cout << "工资 = " << p->getsalary() << endl;
/*
Person p;
cout << "工资 = " << p.getsalary() << endl;
*/
return 0;
}
我的异常网推荐解决方案:软件开发者薪资,http://www.myexception.cn/other/1391128.html