读书人

一个不知道的异常拜托大家给看看

发布时间: 2012-05-24 11:55:41 作者: rapoo

一个不知道的错误,拜托大家给看看
直接贴代码吧!俺是菜鸟,希望大家批评批评,建议建议!

C/C++ code
#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;}


出现了如下一个错误:
fatal error C1004: unexpected end of file found

没辙了,我猜,在定义类 的私有成员a1的时候没有去 给初值,编译器不知给多大内存。但是当我赋初值的时候就出现了3个错误:

1:illegal pure syntax, must be '= 0'
2:'a1' : pure specifier can only be specified for functions
3:unexpected end of file found

我又想是不是调用函数get的时候有问题呢?但是当我 把函数里面的赋值撤走的时候又出现错误:
unexpected end of file found




希望大家给个意见。

[解决办法]
你类定义的右括号呢};
[解决办法]
一看就是大菜鸟,呶下面的可以运行
C/C++ code
#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
析构函数被调用

读书人网 >C++

热点推荐