| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1920 人关注过本帖
标题:头文件中 析构函数出错
只看楼主 加入收藏
邪七
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2017-10-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:8 
头文件中 析构函数出错
class  Cat
{
public:
    cat()//Tom的构造函数
    {
   //     PItsage=new int ();
    //    PItsweight=new int ();
        PItsage=NULL;
        PItsweight=NULL;
        cout<<"constructing..."<<endl;
        return 0;
    }
   cat(int age,int weight)//有财富猫的构造
    {
        PItsage=new int (age);
        PItsweight=new int (weight);
        cout<<"other constructing..."<<endl;
        return 0;
    }
    copycat(Cat &cat1)//引用复制一只与Tom一样的猫
    {
        PItsage=new int(*(cat1.PItsage));
        PItsweight=new int(*(cat1.PItsweight));
        cout<<"copy constructing..."<<endl;
        return 0;
    }
    ~cat()//析造函数     报错
   {
       cout<<*PItsage<<endl;
       delete(PItsage);
       delete(PItsweight);
       cout<<"destructing..."<<endl;
       return 0;
    }
private:
    int *PItsage;
    int *PItsweight;

}

 错误提示:
error: expected class-name before '(' token|
搜索更多相关主题的帖子: Cat new int cout return 
2018-04-28 19:31
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
注意大小写

btw:这代码……。建议你读读《effective c++》
2018-04-28 19:36
邪七
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2017-10-16
收藏
得分:0 
嗯,刚学
2018-04-28 19:37
邪七
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2017-10-16
收藏
得分:0 
所以能找到错误吗?
2018-04-28 19:46
邪七
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2017-10-16
收藏
得分:0 
class  Cat
{
public:
    Cat()//Tom的构造函数
    {
   //     PItsage=new int ();
    //    PItsweight=new int ();
        PItsage=NULL;
        PItsweight=NULL;
        cout<<"constructing..."<<endl;
    }
   Cat(int age,int weight)//有财富猫的构造
    {
        PItsage=new int (age);
        PItsweight=new int (weight);
        cout<<"other constructing..."<<endl;
    }
    Cat(Cat &cat1)//引用复制一只与Tom一样的猫
    {
        PItsage=new int(*(cat1.PItsage));
        PItsweight=new int(*(cat1.PItsweight));
        cout<<"copy constructing..."<<endl;
    }
    ~Cat()//析造函数     报错
   {
       cout<<*PItsage<<endl;
       delete(PItsage);
       delete(PItsweight);
       cout<<"destructing..."<<endl;
    }
private:
    int *PItsage;
    int *PItsweight;

}
2018-04-28 19:53
邪七
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2017-10-16
收藏
得分:0 
真正的正确代码, 构造函数和析构函数都错了,要和类名相同

还有错误的话,希望大家指出。新手上车,代码不好看勿怪


#include <iostream>
using namespace std;
class  Cat
{
public:
    Cat()//Tom的构造函数
    {
   //     PItsage=new int ();
    //    PItsweight=new int ();
        PItsage=NULL;
        PItsweight=NULL;
        cout<<"constructing..."<<endl;
    }
   Cat(int age,int weight)//有财富猫的构造
    {
        PItsage=new int (age);
        PItsweight=new int (weight);
        cout<<"other constructing..."<<endl;
    }
    copycat(Cat &cat1)//引用复制一只与Tom一样的猫
    {
        PItsage=new int(*(cat1.PItsage));
        PItsweight=new int(*(cat1.PItsweight));
        cout<<"copy constructing..."<<endl;
        return 0;
    }
    ~Cat()//析造函数
   {
       cout<<*PItsage<<endl;
       delete(PItsage);
       delete(PItsweight);
       cout<<"destructing..."<<endl;
    }
private:
    int *PItsage;
    int *PItsweight;

};
int main()
{
    Cat Tom = Cat();
    Cat Fsy = Cat(3,5);
    Cat lisa;
    lisa.copycat(Fsy);
    return 0;
}
2018-04-28 20:04
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:10 
delete 不需要加括号
delete pointer;
类最后要加分号
Cat(Cat &cat1), 这个是标准的四不像, 既不像拷贝构造也不像移动构造
Cat(const Cat &) 或者 Cat(Cat &&)
手头没有ide暂时就找出这么多
2018-04-28 20:07
邪七
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2017-10-16
收藏
得分:0 
cout<<*PItsage<<endl;  去掉
2018-04-28 20:13
彭文zheng
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-4-28
收藏
得分:0 
cout<<*PItsage<<endl;  去掉
2018-04-28 20:33
快速回复:头文件中 析构函数出错
数据加载中...
 
   



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

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