0

can somebody please explain why can't I compile this snippet of the code? I know that this design is very 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

5
  • You can't be bothered to include the error? Commented Jan 8, 2011 at 22:16
  • What error does the compiler report? On which line? Commented Jan 8, 2011 at 22:16
  • Fixed the formatting; you get formatting for code blocks selecting the code and using the {} button on the markdown editor toolbar, or pressing Ctrl+K or prefixing each line with four spaces. Commented Jan 8, 2011 at 22:16
  • You should really say what error you get, and where you get it. If you want to have explained why you get an error obviously it is useful to know what error that is. Commented Jan 8, 2011 at 22:17
  • 1
    Maybe it's your formatting, but I don't see a closing brace for class Number. Please edit the question to add the proper code. Commented Jan 8, 2011 at 22:19

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Wow ! After editing, I see a pure virtual function missing in the number class ` virtual printNumber() = 0;`.
0

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

Comments

0

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).

Comments

0

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?

3 Comments

I don't use this code, I just want to know why can't I compile it.
True or not, this is not an answer.
no but as part of refactoring the code they may also actually not put all the class into one file, and IntClass.cpp might #include DoubleClass.h and then it will compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.