我在运行代码时收到以下错误,似乎我已经尝试了一切方法来消除它们,但似乎没有任何效果。 任何有关我可能做错的事情的建议或解释将不胜感激。以下是错误: 在我的代码“class Integer:public Number”中,它说“!预期的类名” 和“Integer(const Double &d);”它说“!未知类型名称'Double';您的意思是'double'吗?”
这里是Integer.h中可能包含错误的实际代码:
#ifndef INTEGER
#define INTEGER
#include "Number.h"
#include "Double.h"
namespace MyNamespace {
using std::string;
class Double;
class Integer : public Number
{
private:
void create(int i);
bool NaN(string s, int iCount);
bool nan;
public:
//Constructors
Integer();
Integer(int i);
Integer(const Integer &i);
Integer(const Double &d); //ERROR HERE = "Unknown type 'Double'"
Integer(string s);
void equals(int i);
void equals(string s);
Integer add(const Integer &i);
Integer sub(const Integer &i);
Integer mul(const Integer &i);
Integer div(const Integer &i);
Integer add(int i);
Integer sub(int i);
Integer mul(int i);
Integer div(int i);
int toInt() const;
//Print
void printInteger();
// operator overloads
Integer operator + (const Integer &i);
Integer operator - (const Integer &i);
Integer operator * (const Integer &i);
Integer operator / (const Integer &i);
Integer operator = (const Integer &i);
Integer operator = (int i);
Integer operator = (string s);
string toString() const;
bool operator == (const Integer &i);
bool operator == (int i);
bool operator != (const Integer &i);
bool operator != (int i);
bool isNan();
};
}
#endif
Number.h
#ifndef NUMBER
#define NUMBER
#include <iostream>
#include <string>
namespace MyNamespace {
using std::string;
class Number : public string
{
public:
Number();
Number(string s);
};
}
#endif
双.h
#ifndef DOUBLE
#define DOUBLE
#include "Number.h"
#include "Integer.h"
namespace MyNamespace
{
class Integer;
class Double : public Number
{
private:
void create(double d);
bool NaN(string s, int dCount);
bool nan;
public:
// Constructors
Double();
Double(double d);
Double(const Double &d);
Double(const Integer &i); //ERROR HERE = "Unknown type 'Integer'"
Double(string s);
void equals(double d);
void equals(string s);
Double add(const Double &d);
Double sub(const Double &d);
Double mul(const Double &d);
Double div(const Double &d);
Double add(double d);
Double sub(double d);
Double mul(double d);
Double div(double d);
double toDouble() const;
//Print
void printDouble();
// operator overloads
Double operator + (const Double &d);
Double operator - (const Double &d);
Double operator * (const Double &d);
Double operator / (const Double &d);
Double operator = (const Double &d);
Double operator = (double d);
Double operator = (string s);
string toString() const;
bool operator == (const Double &d);
bool operator == (double d);
bool operator != (const Double &d);
bool operator != (double d);
bool isNan();
};
}
#endif
最佳答案
Integer 和 Double 之间存在循环依赖关系。如果您不使用这些内联,您可能只有一个前向声明并删除#include“Integer.h”和#include“Double.h”。
如果您想使用这些内联,您可能会包含用于内联实现的其他文件:
#ifndef NUMBER_H
#define NUMBER_H
#include <iostream>
#include <string>
namespace MyNamespace {
// Please do not put the using into the global namespace
using std::string;
class Number : public string {};
}
#endif
// =============================================================================
#ifndef INTEGER_H
#define INTEGER_H
#include "Number.h"
namespace MyNamespace {
class Double;
class Integer : public Number
{
public:
Integer(const Double &d);
};
}
#endif
#include "Integer.tcc"
// =============================================================================
// Integer.tcc
#ifndef INTEGER_H
#error Please include Integer.h instead
#endif
#include "Double.h"
namespace MyNamespace {
inline Integer::Integer(const Double &d) {}
}
// =============================================================================
#ifndef DOUBLE_H
#define DOUBLE_H
#include "Number.h"
namespace MyNamespace
{
class Integer;
class Double : public Number
{
public:
Double(const Integer &i);
};
}
#endif
#include "Double.tcc"
// =============================================================================
// Double.tcc
#ifndef DOUBLE_H
#error Please include Double.h instead
#endif
#include "Integer.h"
namespace MyNamespace {
inline Double::Double(const Integer &d) {}
}
关于c++ - 调试继承类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23440230/