为了账号安全,请及时绑定邮箱和手机立即绑定

请问这样写的为什么不可以呢?

#include"stdafx.h"

#include <string.h>

#include <iostream>

using namespace std;


class Dogs

{

public:

Dogs() 

{

m_dogName = "white";

}

Dogs(string _name)

{

m_dogName = _name;

}

string getName()

{

return m_dogName;

}

private:

string m_dogName;

};

int main(void)

{

Dogs dog1("black");

Dogs dog2;


cout << dog1.getName() << endl;

cout << dog2.getName() << endl;

system("pause");

return 0;

}

https://img1.sycdn.imooc.com//5b739c1f0001c26810790495.jpg

提示我不能输出,求大神解答

正在回答

2 回答

#include <string>

#include <iostream>

using namespace std;


class Dogs

{

public:

    Dogs()

    {

        m_dogName = "white";

    }

    Dogs(string _name)

    {

        m_dogName = _name;

    }

    string getName()

    {

        return m_dogName;

    }

private:

    string m_dogName;

};

int main(void)

{

    Dogs dog1("black");

    Dogs dog2;


    cout << dog1.getName() << endl;

    cout << dog2.getName() << endl;

    system("pause");

    return 0;

}把你的头文件改一下就可以了,程序是正确的,string.h  的使用方法和string 头文件的使用方法有区别,你自己百度一下了解一下就可以了
1 回复 有任何疑惑可以回复我~

切换英文输入试试。。。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之封装篇(上)
  • 参与学习       103381    人
  • 解答问题       732    个

封装--面向对象的基石,本教程力求帮助小伙伴们即学即会

进入课程

请问这样写的为什么不可以呢?

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号