注册 登录
编程论坛 C++教室

头文件中 析构函数出错

邪七 发布于 2018-04-28 19:31, 2185 次点击
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|
8 回复
#2
rjsp2018-04-28 19:36
注意大小写

btw:这代码……。建议你读读《effective c++》
#3
邪七2018-04-28 19:37
嗯,刚学
#4
邪七2018-04-28 19:46
所以能找到错误吗?
#5
邪七2018-04-28 19:53
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;

}
#6
邪七2018-04-28 20:04
真正的正确代码, 构造函数和析构函数都错了,要和类名相同

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


#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;
}
#7
Jonny02012018-04-28 20:07
delete 不需要加括号
delete pointer;
类最后要加分号
Cat(Cat &cat1), 这个是标准的四不像, 既不像拷贝构造也不像移动构造
Cat(const Cat &) 或者 Cat(Cat &&)
手头没有ide暂时就找出这么多
#8
邪七2018-04-28 20:13
cout<<*PItsage<<endl;  去掉
#9
彭文zheng2018-04-28 20:33
cout<<*PItsage<<endl;  去掉
1