一个不知道的错误,拜托大家给看看
直接贴代码吧!俺是菜鸟,希望大家批评批评,建议建议!
#include<iostream>#include<string.h>using namespace std;class people{public:string name;people(){ cout<<"构造fuction!"<<endl;}~people(){ cout<<"析构函数被调用"<<endl;}int get(string a){ a1=a;}int show(){ cout<<a1<<"name"<<name<<"age"<<endl;}private: string a1; int age; int main(){ people yuan; yuan.name="yuan fan"; yuan.get("hellow world!"); yuan.age=15; yuan.show; return 0;}#include<iostream>#include<string.h>using namespace std;class people{public: string name; people() { cout<<"构造fuction!"<<endl; } ~people() { cout<<"析构函数被调用"<<endl; } void get(string a) { a1=a; } void show() { cout<<a1.c_str()<<"name"<<name.c_str()<<"age"<<endl; }public: string a1; int age; }; int main() { people yuan; yuan.name="yuan fan"; yuan.get("hellow world!"); yuan.age=15; yuan.show(); return 0; }
[解决办法]
哥们,好好看看书吧,类定义的括号丢了,而且私有数据成员不能在类外赋值的
[解决办法]
楼上,错误还是比较多的。第一、缺少类定义的右括号呢};,第二、int age应该为public才能在主程序调用,第三、#include<string.h>改为#include<string>,第四、在定义get 和show没有返回值。
我将修改后的程序贴给你:
#include<iostream>
#include<string>
using namespace std;
class people
{
private:
string a1;
public:
string name;
int age;
people()
{
cout<<"构造fuction!"<<endl;
}
~people()
{
cout<<"析构函数被调用"<<endl;
}
int get(string a)
{
a1=a;
return 0;
}
int show()
{
cout<<"name"<<a1<<"age"<<name<<endl;
return 0;
}
};
int main()
{
people yuan;
yuan.name="yuan fan";
yuan.get("hellow world!");
yuan.age=15;
yuan.show();
return 0;
}
输出结果为:
构造fuction!"
namehellow world!age yuan fan
析构函数被调用