我的文件:
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
只允许使用上述功能。无法在我自己的代码中使用运算符“+”。
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
我需要实现以下功能:
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);
例如,在我的q3.h中,我写道:
#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_ */
我得到下一个错误:
expected constructor, destructor, or type conversion before '&' token q3.h /pl5 line 48 C/C++ Problem
这是什么意思?
谢谢。
最佳答案
这是因为您将所有代码放在 header 中(可能是Java的方式?)。
当您包含poly_subtype.h
时,它包含q3.h
,然后包含poly_subtype.h
试图定义各种类。由于您的包含防护,第二个包含无效。然后,当编译器尝试编译q3.h
时,它不知道Number
类是什么,并生成该错误。
解决此问题的方法是将您的方法实现放入仅编译一次的源文件中。它将防止嵌套包含问题。或者,我完全看不到poly_subtype.h
为什么需要包含q3.h
。如果删除该包含,它可能会解决。
另外请注意,IntClass::addInt
通过引用返回一个本地,尽管它似乎可以正常工作,但这绝对是不正确的。您应该改为return *this;
,它是被修改的实际对象(我相信-很难从您的小样本中看出)。