C++ 调试继承类

C++ 调试继承类,c++,class,visual-c++,inheritance,C++,Class,Visual C++,Inheritance,在运行代码时,我收到了以下错误,似乎我已经尽了一切努力来消除它们,但似乎什么都不起作用如果您对我可能做错的事情提出任何建议或解释,我们将不胜感激。以下是错误: 在我的代码“类整数:公共编号”中,它说“!预期的类名” 在“Integer(constdouble&d)”,它说“!未知类型名‘Double’;你是说‘Double’?” 以下是Integer.h中的实际代码,其中包含错误: #ifndef INTEGER #define INTEGER #include "Number.h" #inc

在运行代码时,我收到了以下错误,似乎我已经尽了一切努力来消除它们,但似乎什么都不起作用如果您对我可能做错的事情提出任何建议或解释,我们将不胜感激。以下是错误: 在我的代码“类整数:公共编号”中,它说“!预期的类名” 在“Integer(constdouble&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
#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

我敢打赌,数字和Double(如果有定义的话)是在名称空间中定义的,需要限定。

Simplify Number.h。不要包含“Integer.h”和“Double.h”。您甚至没有引用此文件中这些文件中的任何内容

#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之间存在循环依赖关系。如果不使用这些内联语句,您可能只需要一个前向声明,并删除#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) {}
}
\ifndef编号\u H
#定义数字
#包括
#包括
名称空间MyNamespace{
//请不要将using放入全局命名空间
使用std::string;
类号:公共字符串{};
}
#恩迪夫
// =============================================================================
#ifndef整数
#定义整数
#包括“Number.h”
名称空间MyNamespace{
二等舱;
类整数:公共编号
{
公众:
整数(constdouble&d);
};
}
#恩迪夫
#包括“Integer.tcc”
// =============================================================================
//整数.tcc
#ifndef整数
#错误请改为包含整数.h
#恩迪夫
#包括“Double.h”
名称空间MyNamespace{
内联整数::整数(constdouble&d){}
}
// =============================================================================
#ifndef双U H
#定义双_H
#包括“Number.h”
名称空间MyNamespace
{
类整数;
第二类:公众号
{
公众:
双精度(常数整数&i);
};
}
#恩迪夫
#包括“Double.tcc”
// =============================================================================
//双离合器变速箱
#ifndef双U H
#错误请改为包含Double.h
#恩迪夫
#包括“Integer.h”
名称空间MyNamespace{
内联双精度::双精度(常量整数&d){}
}

Number.h和Double.h是什么?学习如何解释错误消息非常重要。编译器告诉您它不知道数字和Double是什么意思。特别是,它告诉你Double是一种未知类型。为什么会这样呢?找出你的理解与编译器的不同之处。那么,为什么你认为它应该知道双倍?你定义/声明了吗?哪里定义/声明是否正确?有打字错误吗?正确的名称空间?我怀疑您在Double.h中使用了
#include“Integer.h”
。如果是这种情况,您需要从Double.h中删除该行,并从Integer.h中删除
#include“Double.h”
。只需在每个文件中使用前向声明。您使用的是两个名称空间--
Roman
MyNamespace
--您必须使类完全限定,包括名称空间,以便跨名称空间使用它们。您还遇到了R Sahu提到的循环包含问题。现在一切都正常。请接受“Double(const Integer&i);”现在是“未知类型名‘Integer’”@user1您是说“除了”吗?如果你这样做了,除了什么?你必须提供一个双精度的
Integer
的前向声明。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) {}
}