My files:
cpu_add.h
#ifndef CPU_ADD_H
#define CPU_ADD_H
double add_double_double(double a, double b) {return (a+b);}
double add_int_double(int a, double b) {return ((double)(a)+b);}
int add_int_int(int a, int b) {return (a+b);}
#endif
I am allowed only to use the functions above. Can’t use the operator ‘+’ in my own code.
poly_subtype.h
#ifndef POLY_SUBTYPE_H
#define POLY_SUBTYPE_H
#include <iostream>
#include "q3.h"
#include "cpu_add.h"
using std::cout;
using std::endl;
//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;
//Print the number stored in the object
virtual void print_number() = 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;}
//print the number stored in the object
void print_number() {cout << my_number << endl;}
//return a DoubleClass object that's the result of x+this
Number& addDouble(DoubleClass& x);
//return an IntClass object that's the result of x+this
Number& addInt(IntClass& x);
//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);
};
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;}
//Print the number stored in the object
void print_number() {cout << my_number << endl;}
//return a DoubleClass object that's the result of x+this
Number& addDouble(DoubleClass& x);
//return a DoubleClass object that's the result of x+this
Number& addInt(IntClass& x);
//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);
};
#endif
I need to implement the next functions:
Number& IntClass::addInt(IntClass& x);
Number& IntClass::addDouble(DoubleClass& x);
Number& IntClass::operator+(Number& x);
Number& DoubleClass::addInt(IntClass& x);
Number& DoubleClass::addDouble(DoubleClass& x);
Number& DoubleClass::operator+(Number& x);
So for example in my the q3.h I wrote:
#ifndef Q3_H_
#define Q3_H_
#include "cpu_add.h"
#include "poly_subtype.h"
Number& IntClass::addInt(IntClass& x){
IntClass num(add_int_int(my_number, x.get_number()));
return num;
}
#endif /* Q3_H_ */
and I get the next error:
expected constructor, destructor, or type conversion before '&' token q3.h /pl5 line 48 C/C++ Problem
What does it mean?
Thanks.
This is caused because you put all your code in headers (maybe thinking the Java way?).
When you include
poly_subtype.hit includesq3.hwhich then includespoly_subtype.hin an attempt to define various classes. Due to your include guards the second include has no effect. Then when the compiler attempts to compileq3.hit has no idea what theNumberclass is, and generates that error.The fix to this is to put your method implementations into source files that are only compiled once. It will prevent the nested inclusion problems. Alternately I can’t see why
poly_subtype.hneeds to includeq3.hat all. If you remove that include it might fix it.Also as a side note,
IntClass::addIntreturns a local by reference which is definitely incorrect although it may appear to work. You should insteadreturn *this;which is the actual object being modified (I believe – it’s hard to tell from your small sample).