标题:帮忙看看这个程序的错误
只看楼主
袁广川
Rank: 2
等 级:论坛游民
帖 子:21
专家分:25
注 册:2016-11-12
结帖率:0
已结贴  问题点数:20 回复次数:4 
帮忙看看这个程序的错误
#include <iostream>
#include <string.h>
using namespace std;
class Girl{
    private :
        string*name;
        int age;
    public :
        Girl(string*n,int d)
        {
            name=new string[strlen(n)+1];
            name=n;
            age=d;
        }
        friend void disp(Girl&);
        ~Girl()
        {
            delete []name;
        }
};
void disp(Girl&x)
{
    cout<<"The girl's name is "<<x.name<<",age is "<<x.age<<"."<<endl;
}
int main()
{
    string name;
    cin>>name;
    int age;
    cin>>age;
    Girl g1(name,age);
    disp(g1);
    return 0;
}
搜索更多相关主题的帖子: private include public friend 
2017-03-28 21:29
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:10 
如果是编译失败,应该贴出编译错误,比如
error C2664: 'strlen' : cannot convert parameter 1 from 'std::string *' to 'const char *'
实在是太瞎搞了,比如
name=new string[strlen(n)+1];
name=n;
根本无法猜出你想干什么,只能重写一个
程序代码:
#include <iostream>
#include <string>

class Girl
{
public:
    Girl() : name_(), age_()
    {
    }
    Girl( const std::string& name, unsigned age ) : name_(name), age_(age)
    {
    }

private:
    std::string name_;
    unsigned age_;

    friend std::istream& operator>>( std::istream& is, Girl& girl );
    friend std::ostream& operator<<( std::ostream& os, const Girl& girl );
};

using namespace std;

std::istream& operator>>( std::istream& is, Girl& girl )
{
    return is >> girl.name_ >> girl.age_;
}

std::ostream& operator<<( std::ostream& os, const Girl& girl )
{
    return os << "The girl's name is " << girl.name_ << ", age is " << girl.age_ << ".";
}

int main( void )
{
    Girl girl;
    cin >> girl;
    cout << girl << endl;

    return 0;
}
2017-03-29 08:36
袁广川
Rank: 2
等 级:论坛游民
帖 子:21
专家分:25
注 册:2016-11-12
得分:0 
回复 2楼 rjsp
#include <iostream>
#include <string.h>
using namespace std;
class Girl{
    private :
        char * name;
        int age;
    public :
        Girl(char * n,int d)
        {
            name=new char[strlen(n)+1];
            strcpy(name,n);
            age=d;
        }
        friend void disp(Girl&x);
        ~Girl()
        {
            delete []name;
        }
};
void disp(Girl&x)
{
    cout<<"The girl's name is "<<x.name<<",age is "<<x.age<<"."<<endl;
}
int main()
{
    char * name1;
    cin>>name1;
    int age;
    cin>>age;
    Girl g1(name1,age);
    disp(g1);
    return 0;
}


//这个错误在哪里?
2017-03-30 18:01
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:10 
char * name1;
改成 char name1[20];//长度根据自己需要指定
2017-03-30 18:17
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
除了这个明显的错误,还缺拷贝构造和赋值函数,否则会重复释放内存。
当然,最大的问题是一点儿都不C++,四不像
2017-03-30 21:48



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




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

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