| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1370 人关注过本帖
标题:运行报错
只看楼主 加入收藏
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
运行报错
头文件
#ifndef COW_H_
#define COW_H_
namespace COW
{
    class Cow
    {
    private:
        char name[20];
        char* hoppy;
        double weight;
    public:
        Cow();
        Cow(const char* nm, const char* ho, double wt);
        Cow(const Cow& c);
        ~Cow();
        Cow& operator=(const Cow& c);
        void ShowCow()const;
    };
}
#endif
源代码1
#include"stack.h"
#include<iostream>
#include<cstring>
namespace COW
{
    Cow::Cow()
    {
        const char* temp = "no date";
        hoppy = new char(strlen(temp) + 1);
        for (int i = 0; i < strlen(temp)+1 ; i++)
            name[i] = hoppy[i] = temp[i];
        name[strlen(temp) ] = hoppy[strlen(temp) ] = '\0';
        weight = 0;
    }
    Cow::Cow(const char* nm, const char* ho, double wt)
    {
        for (int i = 0; i < strlen(nm) + 1; i++)
            name[i] = nm[i];
        name[strlen(nm) + 1] = '\0';
        hoppy = new char(strlen(ho) + 1);
        for (int i = 0; i < strlen(ho)+1; i++)
            hoppy[i] = ho[i];
        hoppy[strlen(ho)] = '\0';
        weight = wt;
    }
    Cow::Cow(const Cow& c)
    {
        for (int i = 0; i < strlen(c.name); i++)
            name[i] = c.name[i];
        name[strlen(c.name) + 1] = '\0';
        hoppy = new char(strlen(c.hoppy) + 1);
        for (int i = 0; i < strlen(c.hoppy)+1; i++)
            hoppy[i] = c.hoppy[i];
        hoppy[strlen(c.hoppy)] = '\0';
        weight = c.weight;
    }
    Cow::~Cow()
    {
        std::cout << name << " hoppy:" << hoppy << " object had deleted\n";
        delete[]hoppy;
    }
    Cow& Cow::operator=(const Cow& c)
    {
        if (this == &c)
            return *this;
        delete[]hoppy;
        for (int i = 0; i < strlen(c.name); i++)
            name[i] = c.name[i];
        name[strlen(c.name) + 1] = '\0';
        hoppy = new char(strlen(c.hoppy) + 1);
        for (int i = 0; i < strlen(c.hoppy); i++)
            hoppy[i] = c.hoppy[i];
        hoppy[strlen(c.hoppy) + 1] = '\0';
        weight = c.weight;
    }
    void Cow::ShowCow()const
    {
        std::cout << "Name:" << name << std::endl;
        std::cout << "Hoppy:" << hoppy << std::endl;
        std::cout << "Weight:" << weight << std::endl;
    }
}
源代码2
#include<iostream>
#include"stack.h"
int main()
{
    {
        using std::cout;
        using std::cin;
        using std::endl;
        using COW::Cow;
        Cow st1;
        
        Cow st2("chen", "music", 60);
        
        Cow st3 = st2;
        st1.ShowCow();
        st2.ShowCow();
        st3.ShowCow();
    }
    std::cout << "Done!\n";
    return 0;
}
图片附件: 游客没有浏览图片的权限,请 登录注册

我想的是最后结束后
显示:
 chen music objected had deleted
 chen music objected had deleted
no data no data objected had deleted
Done!
搜索更多相关主题的帖子: const strlen char name std 
2021-06-01 21:22
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:10 
你是不是将 new char[……] 错写成 new char(……) 了?一共有4处需要修改。
另外,Cow& Cow::operator=(const Cow& c) 的实现怎么没有 return *this; ?
2021-06-02 08:31
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:10 
还有所有类似 name[strlen(c.name) + 1] = '\0'; 这样的语句,也是错的。
应该是 name[strlen(c.name)] = '\0';
2021-06-02 08:41
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:0 
程序代码:
#include <iostream>
#include <cstring>

class Cow
{
public:
    Cow() : name_(), hoppy_(), weight_()
    {
    }
    Cow( const char* name, const char* hoppy, double weight ) : hoppy_(strdup(hoppy)), weight_(weight)
    {
        strncpy( name_, name, sizeof(name_)-1 );
        name_[sizeof(name_)-1] = '\0';
    }
    Cow( const Cow& obj ) : hoppy_(strdup(obj.hoppy_)), weight_(obj.weight_)
    {
        strcpy( name_, obj.name_ );
    }
    Cow& operator=( const Cow& obj )
    {
        if( this != &obj )
        {
            delete[] hoppy_;

            strcpy( name_, obj.name_ );
            hoppy_ = strdup(obj.hoppy_);
            weight_ = obj.weight_;
        }
        return *this;
    }
    ~Cow()
    {
        std::clog << *this << " has been deleted.\n";
        delete[] hoppy_;
    }
    
    friend std::ostream& operator<<( std::ostream& os, const Cow& obj )
    {
        if( obj.hoppy_ )
            return os << "{ name=\"" << obj.name_ << "\", hoppy=\"" << obj.hoppy_ << "\", weight=" << obj.weight_ << " }";
        return os << "{ name=\"<no date>\", hoppy=\"<no date>\", weight=0 }";
    }

private:
    char name_[20];
    char* hoppy_;
    double weight_;
};

using namespace std;

int main( void )
{
    {
        Cow c1;
        cout << c1 << endl;

        Cow c2("chen", "music", 60);
        cout << c2 << endl;

        Cow c3 = c2;
        cout << c3 << endl;

        c3 = c2;
        cout << c3 << endl;

        Cow c4("12345678901234567890", "music", 60);
        cout << c4 << endl;
    }

    std::cout << "Done!\n";
}


输出
{ name="<no date>", hoppy="<no date>", weight=0 }
{ name="chen", hoppy="music", weight=60 }
{ name="chen", hoppy="music", weight=60 }
{ name="chen", hoppy="music", weight=60 }
{ name="1234567890123456789", hoppy="music", weight=60 }
{ name="1234567890123456789", hoppy="music", weight=60 } has been deleted.
{ name="chen", hoppy="music", weight=60 } has been deleted.
{ name="chen", hoppy="music", weight=60 } has been deleted.
{ name="<no date>", hoppy="<no date>", weight=0 } has been deleted.
Done!
2021-06-02 09:15
快速回复:运行报错
数据加载中...
 
   



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

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