开发者

compiling error on C++

开发者 https://www.devze.com 2023-02-03 18:30 出处:网络
can somebody please explain why can\'t I compile this snippet of the code? I know that this design is ver开发者_如何学Goy bad, but I just want to know why I can\'t compile it, thanks in advance

can somebody please explain why can't I compile this snippet of the code? I know that this design is ver开发者_如何学Goy bad, but I just want to know why I can't compile it, thanks in advance P.S. sorry for the format, can't find backquotes on the panel

//Deriving classes definition 
class IntClass; class DoubleClass;

//The Virtual Number Class. IntClass and FloatClass will derive from this class.

class Number {
    public:
        //return a Number object that's the results of x+this, when x is DoubleClass
        virtual Number& addDouble(DoubleClass& x) = 0;

        //return a Number object that's the results of x+this, when x is IntClass
        virtual Number& addInt(IntClass& x) = 0;

        //return a Number object that's the results of x+this, when x is either
        //IntClass or DoubleClass
        virtual Number& operator+(Number& x) = 0;
};

class IntClass : public Number {
    private:
        int my_number;
    public:
        //Constructor
        IntClass(int n):my_number(n) {}

        //returns the number stored in the object
        int get_number()  {return my_number;}

        //return a DoubleClass object that's the result of x+this
        Number& addDouble(DoubleClass& x){
         return  x.addInt(*this);
        }

        //return an IntClass object that's the result of x+this
        Number& addInt(IntClass& x){
         IntClass* var = new IntClass(my_number + x.get_number());
         return  *var;
        }

        //return a Number object that's the result of x+this.
        //The actual class of the returned object depends on x.
        //If x is IntClass, then the result if IntClass.
        //If x is DoubleClass, then the results is DoubleClass.
        Number& operator+(Number& x){
         return x.addInt(*this);
        }
};

class DoubleClass : public Number {
    private:
        double my_number;
    public:
        //Constructor
        DoubleClass(double n):my_number(n) {}

        //returns the number stored in the object
        double get_number()  {return my_number;}

        //return a DoubleClass object that's the result of x+this
        Number& addDouble(DoubleClass& x){
         DoubleClass* var = new DoubleClass(my_number + x.get_number());
         return *var;
        }

        //return a DoubleClass object that's the result of x+this
        Number& addInt(IntClass& x){
         DoubleClass* var = new DoubleClass(my_number + x.get_number());
         return *var;
        }

        //return a DoubleClass object that's the result of x+this.
        //This should work if x is either IntClass or DoubleClass
        Number& operator+( Number& x){
         return x.addDouble(*this);
        }
};

I have error in the IntClass in addDouble method:

invalid use of undefined type struct DoubleClass

Edited IntClass is not nested class of the NumberClass


Inside IntClass::addDouble, you use the class DoubleClass, but at that point DoubleClass has only a forward declaration, so you can't call methods on it.

This can be fixed by putting the body of IntClass::addDouble after the full declaration of class DoubleClass, or by separating your code into header and implementation files.


If you have a pure virtual function in the base class, then the derived class must implement it.


If the all derived classed you must implement your pure virtual function (where set = 0 in the end), such as addDouble, addInt, Number& operator+(Number& x) = 0, print_number

else you will have errors in derived classed

write implement of pure virtual function in IntClass and DoubleClass


You have a visibility problem because IntClass is a nested class of Number and DoubleClass is instead a regular class but it's used before its declaration (so the word DoubleClass at line 8 doesn't mean anything for the compiler).

A solution could be adding two forwards at the beginning and then making all three classes normal top-level classes by adding a closing brace before the definition of IntClass.

class IntClass;
class DoubleClass;

However there will be a problem with the inline implementation of IntClass::addDouble because at that point DoubleClass is only known to be a class (it's an incomplete type). By separating the implementation and moving it after the definition of DoubleClass you can compile.

Note that this code that now compiles is still very bad (in particular it's leaking).


Refactor the code, so each class declaration goes into its own header file, Number.h, IntClass.h DoubleClass.h and the implementations go into compilation units IntClass.cpp and DoubleClass.cpp.

Then get IntClass.cpp to #include DoubleClass.h because it uses that class.

Then it will compile.

However this code is going to leak terribly and is totally non-const-correct. These issues also need to be addressed. Perhaps return smart-pointers instead of references, or an external "holder" class that manages the lifetime of the pointers underneath it properly.

And why are you rewriting a numeric system?

0

精彩评论

暂无评论...
验证码 换一张
取 消