调试继承类 [英] Debugging Inheritance Class
问题描述
在运行代码时,我收到以下错误,似乎我已尽一切努力消除了这些错误,但似乎没有任何效果。 任何建议或对我可能做错事的解释将不胜感激。以下是错误:
在我的 class Integer:public Number代码中,它表示 !!期望的类名
和位于 Integer(const Double& d);它说:!未知类型名称'Double';您是说'double'吗?
I am receiving the following errors when running my code and it seems as if I had tried everything to eliminate them but nothing is appearing to work. Any suggestions or explanation of what I be may doing wrong would be greatly appreciated. Here are the errors: In my code at "class Integer : public Number" it says "! Expected class name" and at "Integer(const Double &d);" it says "! Unknown type name 'Double'; did you mean 'double'?"
以下可能是 Integer.h 中的实际代码其中包含以下错误:
Here is may actual code in Integer.h which contains the errors:
#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
Double.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
推荐答案
简化Number.h。不要在其中包含 Integer.h和 Double.h。您甚至都没有引用此文件中的那些文件。
Simplify Number.h. Don't include "Integer.h" and "Double.h" in it. You are not even referencing anything from those files in this file.
#ifndef NUMBER // I'd recommend using NUMBER_H instead of just NUMBER
#define NUMBER
#include <iostream>
#include <string>
using std::string;
namespace MyNamespace {
class Number : public string
{
public:
Number();
Number(string s);
};
}
#endif
删除< Double.h的code> #include Integer.h 。同样,从Integer.h中删除 #include Double.h
。在Double.h中添加 Integer
的前向声明,并在Integer.h中添加 Double
的前向声明。
Remove #include "Integer.h"
from Double.h. Similarly, remove #include "Double.h"
from Integer.h. Add a forward declaration of Integer
in Double.h and a forward declaration of Double
in Integer.h.
#ifndef DOUBLE
#define DOUBLE
#include "Number.h"
namespace MyNamespace
{
class Integer;
这篇关于调试继承类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!