标题:请教一个c++类的问题,这个程序错在哪儿啊?运行时无法给字符串赋值
取消只看楼主
蓝天绿水
Rank: 2
等 级:论坛游民
威 望:1
帖 子:24
专家分:29
注 册:2017-9-16
结帖率:66.67%
已结贴  问题点数:20 回复次数:2 
请教一个c++类的问题,这个程序错在哪儿啊?运行时无法给字符串赋值
程序代码:
#include <iostream.h>

using namespace std;
class node 
{
    private:
        int ID;
        char *name;
        int Tel;
    public:
        node ()
        {
            ID=0;name=0;Tel=0;
        }
        node (int a,char *b,int c)
        {
            ID=a;name=b;Tel=c;
        }
        void setnode()
        {
            cout<<"input ID: "<<endl;
            cin>>ID;
            cout<<"input name: "<<endl;
            cin>>name;   //这一行代码一直运行出错 
            cout<<"input Tel: "<<endl;
            cin>>Tel;
        }
        void output()
        {
            cout<<ID<<" "<<name<<" "<<Tel<<endl;
        }
        ~node(){}
};
int main ()
{
    node zx;
    zx.setnode();
    zx.output();
    return 0;
}
搜索更多相关主题的帖子: 运行 name node int cout 
2017-09-18 22:45
蓝天绿水
Rank: 2
等 级:论坛游民
威 望:1
帖 子:24
专家分:29
注 册:2017-9-16
得分:0 
那应该怎么改啊,我就是想让用户自己输出字符串再赋值给name,但是一直出错
2017-09-19 12:48
蓝天绿水
Rank: 2
等 级:论坛游民
威 望:1
帖 子:24
专家分:29
注 册:2017-9-16
得分:0 
哦,懂了,是因为类在申明是并未分配内存,谢谢
2017-09-19 19:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-480791-1-1.html




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

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