Debugging Inheritance Class

2019-07-20 00:11发布

问题:

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'?"

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

回答1:

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

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;


回答2:

You have a circular dependency between Integer and Double. If you do not utilize these inline you might just have a forward declarations and remove the #include "Integer.h" and #include "Double.h".

If you want to use these inline you might include additional files for the inline implementations:

#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) {}
}


回答3:

I would wager that Number and Double, if defined at all, are defined in a namespace and need to be qualified.