| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 987 人关注过本帖
标题:刚学C++,请帮我看看下面这个程序,多谢。。。。
只看楼主 加入收藏
暗藏
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-8-18
结帖率:66.67%
收藏
已结贴  问题点数:0 回复次数:5 
刚学C++,请帮我看看下面这个程序,多谢。。。。
# include <iostream.h>
# include <string.h>
class CPerson
{
protected:
    string name;
    int age;
public:
    CPerson(string n,int a);
    CPerson ()
    {
        cout<<"\n什么也不能做的CPerson构造函数\n";}
    void WritePerson()
    {
        cout<< name<<"年龄为"<<age<<"\n";}
        ~CPerson()
        {
            cout<<"\n销毁CPerson对象\n";
        }
    };
    CPerson::CPerson (string n,int a)
    {
        cout<<"\n在CPerson构造函数中\n";
        name=n;
        age=a;
        WritePerson();
        cout<<"离开CPerson构造函数\n";
    }
    int main()
    {
        CPerson somebody("Vicent",29);
        cout<<"\n没有人了\n\n";
        return 0;
    }
我用VC++6.0编译时出现了以下错误提示:
:\vc\MSDev98\MyProjects\first18\01.cpp(6) : error C2146: syntax error : missing ';' before identifier 'name'
F:\vc\MSDev98\MyProjects\first18\01.cpp(6) : error C2501: 'string' : missing storage-class or type specifiers
F:\vc\MSDev98\MyProjects\first18\01.cpp(6) : error C2501: 'name' : missing storage-class or type specifiers
F:\vc\MSDev98\MyProjects\first18\01.cpp(9) : error C2629: unexpected 'class CPerson ('
F:\vc\MSDev98\MyProjects\first18\01.cpp(9) : error C2238: unexpected token(s) preceding ';'
F:\vc\MSDev98\MyProjects\first18\01.cpp(21) : error C2065: 'string' : undeclared identifier
F:\vc\MSDev98\MyProjects\first18\01.cpp(21) : error C2146: syntax error : missing ')' before identifier 'n'
F:\vc\MSDev98\MyProjects\first18\01.cpp(21) : error C2350: 'CPerson::CPerson::CPerson' is not a static member
F:\vc\MSDev98\MyProjects\first18\01.cpp(21) : error C2059: syntax error : ')'
F:\vc\MSDev98\MyProjects\first18\01.cpp(22) : error C2143: syntax error : missing ';' before '{'
F:\vc\MSDev98\MyProjects\first18\01.cpp(22) : error C2447: missing function header (old-style formal list?)
F:\vc\MSDev98\MyProjects\first18\01.cpp(31) : error C2661: 'CPerson::CPerson' : no overloaded function takes 2 parameters
执行 cl.exe 时出错.

first18.exe - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: 函数 class include public 
2010-08-31 11:51
mxs810
Rank: 9Rank: 9Rank: 9
来 自:火星
等 级:贵宾
威 望:16
帖 子:234
专家分:1122
注 册:2006-10-19
收藏
得分:2 
# include <iostream>
# include <string>

using namespace std;


class CPerson
{
protected:
    string name;
    int age;
public:
    CPerson(string n,int a);
    CPerson ()
    {
        cout<<"\n什么也不能做的CPerson构造函数\n";}
    void WritePerson()
    {
        cout<<name.c_str()<<"年龄为"<<age<<"\n";}
        ~CPerson()
        {
            cout<<"\n销毁CPerson对象\n";
        }
    };
    CPerson::CPerson (string n,int a)
    {
        cout<<"\n在CPerson构造函数中\n";
        name=n;
        age=a;
        WritePerson();
        cout<<"离开CPerson构造函数\n";
    }
    int main()
    {
        CPerson somebody("Vicent",29);
        cout<<"\n没有人了\n\n";
        return 0;
    }

学习一下有关名字空间的东东~~

授人以鱼不如授人以渔
2010-08-31 13:22
暗藏
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-8-18
收藏
得分:0 
可否告诉我为什么要这么改,多谢了。
2010-08-31 14:24
x_wangyue
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:34
专家分:144
注 册:2010-8-30
收藏
得分:2 
# include <iostream.h>
# include <string.h>
using namespace srd;//这里加上命名空间

class CPerson
{
protected:
    string name;
    int age;
public:
    CPerson(string n,int a);
    CPerson ()
    {
        cout<<"\n什么也不能做的CPerson构造函数\n";}
    void WritePerson()
    {
        cout<< name<<"年龄为"<<age<<"\n";}
        ~CPerson()
        {
            cout<<"\n销毁CPerson对象\n";
        }
    };
    CPerson::CPerson (string n,int a)
    {
        cout<<"\n在CPerson构造函数中\n";
        name=n;
        age=a;
        WritePerson();
        cout<<"离开CPerson构造函数\n";
    }
    int main()
    {
        CPerson somebody("Vicent",29);
        cout<<"\n没有人了\n\n";
        return 0;
    }
2010-08-31 21:03
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:2 
程序代码:
#include<iostream>
#include<string>
using namespace std;

class CPerson
{
protected:
    string name;
    int age;
public:
    CPerson(string n,int a);
    CPerson ()
    {
        cout<<"\n什么也不能做的CPerson构造函数\n";
    }
    void WritePerson()
    {
        cout<< name<<"年龄为"<<age<<"\n";
    }
     ~CPerson()
     {
         cout<<"\n销毁CPerson对象\n";
     }
   
};
    CPerson::CPerson (string n,int a)
    {
        cout<<"\n在CPerson构造函数中\n";
        name=n;
        age=a;
        WritePerson();
        cout<<"离开CPerson构造函数\n";
    }


    int main()
    {
        CPerson somebody("Vicent",29);
        cout<<"\n没有人了\n\n";
        return 0;
    }
这样就可以了

南国利剑
2010-08-31 23:50
ragnaros
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:15
专家分:105
注 册:2010-7-26
收藏
得分:2 
# include <iostream.h>
# include <string.h>
改成
#include<iostream>
#include<string>
using namespace std;

<iostream>表示你使用的是标注命名空间,也就是在程序开始应该有这么一句话
using namespace std ;
这是遵循c++标准的
<iostream.h>
则没有遵循c++标准
<string.h>是旧的C头文件,对应的是基于char*的字符串处理函数;
<string>是包装了std的C++头文件,对应的是新的strng类;
2010-09-01 08:55
快速回复:刚学C++,请帮我看看下面这个程序,多谢。。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012269 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved