注册 登录
编程论坛 C语言论坛

类和对象

FrankloveCyy 发布于 2019-07-08 11:17, 1650 次点击
#include<iostream>
#include<string>

class Human
{
    public:
        string name;
        int age;
        
        void IntroduceSelf()
        {
            cout<<"I am" + name <<"and am";
            cout<<age<<"yeas old"<<endl;
        }
};
int main()
{
    //An object of class Human with atttibute name as "Adam
    Human firstMan;
    firstMan.name="Adam";
    firstMan.age= 30;
   
    //An object of class Human with attribuete name as "Eve"
    Human firstWoman;
    firstWoman.age=28;
   
    firstMan.IntroduceSelf();
    firstWoman.IntroduceSelf();
}
只有本站会员才能查看附件,请 登录
这是编译器出现的报错,代码都是照的书上代码敲的,也没有单词敲错啊,大神们能告诉我原因吗?编译器用的 Dev-C++ 5.11
2 回复
#2
wufuzhang2019-07-08 11:32
回复 楼主 FrankloveCyy
#include<iostream>
#include<string>

class Human
{
    public:
        std::string name;
        int age;
        
        void IntroduceSelf()
        {
            std::cout<<"I am" + name <<"and am";
            std::cout<<age<<"yeas old"<<std::endl;
        }
};
int main()
{
    //An object of class Human with atttibute name as "Adam
    Human firstMan;
    firstMan.name="Adam";
    firstMan.age= 30;
   
    //An object of class Human with attribuete name as "Eve"
    Human firstWoman;
    firstWoman.age=28;
   
    firstMan.IntroduceSelf();
    firstWoman.IntroduceSelf();
}


在前面加上std::就可以了
#3
FrankloveCyy2019-07-08 13:57
好的,非常感谢。
1