【问题标题】:"Class" has not been declared“类”尚未声明
【发布时间】:2020-04-16 22:15:42
【问题描述】:

首先,我知道这个问题已经here处理过了,但是一直没能解决。

我有几个类,它们是 LitteralPart、Monome、Polynome 和 PolynomialFraction。它们都必须能够相互使用,(即 Monome 必须能够拥有可以返回 PolynomialFraction 的方法),但是当我尝试编译程序时,我得到了大约 400 个

"Polynome" does not name a type
"Monome" is not declared
"LitteralPart" is not declared in this scope

等等。

每个头文件如下:

#ifndef // Variable for the file
#define // Variable for the file

#include "includes.h" // Contains several includes like <QString> or <QVector>

#include // All others class headers

class Class
{
   // Class definition
};

// Operators overloading

例如,这里是 Polynome 标头,注释中包含所有错误:

#ifndef POLYNOME_H
#define POLYNOME_H

#include "includes.h"

#include "monome.h"
#include "polynomialfraction.h"
#include "math.h"

class Polynome
{
public:
    Polynome();
    Polynome(QString polynome);

    void setMonomeVector(QVector<Monome> monomeVector);
        /*
           "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
        */

    QVector<Monome> getMonomeVector()const;
        /*
           "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
        */

    int getConstantValue();
    QStringList getLitteralParts() const;

    void simplify();
    void invert();

    int getDegree() const;
    QString toString()const;

    void operator+=(Monome const& other);
        /*
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
         */
    void operator+=(Polynome const& other);

    void operator-=(Monome const& other);
        /*
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
         */
    void operator-=(Polynome const& other);

    void operator*=(int const& other);
        /*
            with "void operator*=(int const& other)";
            with "void operator*=(int const& other)";
            with "void operator*=(int const& other)";
            with "void operator*=(int const& other)";
            with "void operator*=(int const& other)";
         */
    void operator*=(Monome const& other);
        /*
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
         */
    void operator*=(Polynome const& other);

    void operator/=(int const& other);
        /*
            with "void operator/=(int const& other)";
            with "void operator/=(int const& other)";
            with "void operator/=(int const& other)";
            with "void operator/=(int const& other)";
            with "void operator/=(int const& other)";
        */
    void operator/=(Monome const& other);
        /*
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
            "Monome" has not been declared
         */
    void operator/=(Polynome const& other);

    void operator=(QString const& value);
    void operator=(const char* value);

private:
    QVector<Monome> monomeVector;
        /*
           "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
            "Monome" was not declared in this scope
            template argument 1 is invalid
        */
};

Polynome operator+(Polynome const& a, Monome const& b);
    /*
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
    */
Polynome operator+(Polynome const& a, Polynome const& b);

Polynome operator-(Polynome const& a, Monome const& b);
    /*
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
     */
Polynome operator-(Polynome const& a, Polynome const& b);
Polynome operator*(Polynome const& a, int const& b);
Polynome operator*(int const& b, Polynome const& a);
Polynome operator*(Polynome const& a, int const& b);
Polynome operator*(Polynome const& a, Monome const& b);
    /*
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
     */
Polynome operator*(Polynome const& a, Polynome const& b);

Polynome operator/(Polynome const& a, int const& b);
Polynome operator/(int const& b, Polynome const& a);
Polynome operator/(Polynome const& a, Monome const& b);
    /*
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
     */
Polynome operator/(Monome const& b, Polynome const& a);
    /*
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
        "Monome" has not been declared
     */
Polynome operator/(Polynome const& a, Polynome const& b);

#endif // POLYNOME_H

所有其他文件都存在同样的问题。

除了includes.h 之外的所有这些文件都被放置在项目的子目录中,但如果我使用#include "polynome.h"#include "variables/polynome.h" 甚至它们的绝对路径,则不会有任何改变。

【问题讨论】:

  • 在您发布的代码中,您没有半色 (;) 来结束类定义。这是您发布代码时的拼写错误,还是您计算机上的代码中也缺少这种错误?
  • 似乎是周期性包含的问题。 a.h 包括b.h,其中包括c.h,又包括a.h。你必须以某种方式打破这个循环。
  • 谷歌“c++ 前向声明”以取得成功。请检查拼写,以防万一,正确的术语是“多项式”、“单项式”和“文字”。
  • 前向声明或接口用于打破循环依赖。
  • 不看代码没法详细解释,也不太容易。但我要做的是将所有这些相互关联的类放入一个头文件中。在必要时获取使用前向声明声明的所有类和方法。然后稍后在头文件中定义您想要使用类外显式内联定义定义的任何方法。

标签: c++


【解决方案1】:

我已将(并重命名)我所有的类声明放在一起,所以我有:

#ifndef VARIABLESINCLUDE_H
#define VARIABLESINCLUDE_H

#include "includes.h"
#include "math.h"

class Litteral;
class Monomial;
class Polynomial;
class PolynomialFraction;

class Litteral
{
public:
    Litteral();
    Litteral(QString value);

    QString toString() const;

    void invert();

    void setLitteral(QString litteral);
    void setExposant(int exposant);

    QString getLitteral() const;
    int getExposant() const;

    QString operator+(Litteral a);

private:
    QString litteral;
    int exposant;
};

bool operator==(Litteral const& a, Litteral const& b);
bool operator!=(Litteral const& a, Litteral const& b);

class Monomial
{
public:
    Monomial();
    Monomial(QString monome);

    QStringList getLitterals() const;

    QString toAbsoluteValueString() const;
    QString toString() const;

    void invert();

    void operator*=(int other);
    void operator*=(Litteral other);
    void operator*=(Monomial other);

    void operator/=(int const& other);
    void operator/=(Litteral const& other);
    void operator/=(Monomial const& other);

    void setCoefficient(int coefficient);
    void setLitteralVector(QVector<Litteral> litteralPartVector);
    void addLitteral(Litteral litteralPart);

    bool isNull() const;
    int getMaxPower() const;
    int getDegree() const;
    int getCoefficient() const;
    QVector<Litteral> getLitteralVector() const;

    void simplify();

private:
    QVector<Litteral> litteralPartVector;
    int coefficient;
};

Monomial operator*(Monomial const& a, int const& b);
Monomial operator*(Monomial const& a, Litteral const& b);
Monomial operator*(Monomial const& a, Monomial const& b);

Monomial operator/(Monomial const& a, int const& b);
Monomial operator/(Monomial const& a, Litteral const& b);
Monomial operator/(Monomial const& a, Monomial const& b);

class Polynomial
{
public:
    Polynomial();
    Polynomial(QString polynome);

    void setMonomialVector(QVector<Monomial> monomeVector);

    QVector<Monomial> getMonomialVector()const;
    int getConstantValue();
    QStringList getLitterals() const;

    void simplify();
    void invert();

    int getDegree() const;
    QString toString()const;

    void operator+=(Monomial const& other);
    void operator+=(Polynomial const& other);

    void operator-=(Monomial const& other);
    void operator-=(Polynomial const& other);

    void operator*=(int const& other);
    void operator*=(Monomial const& other);
    void operator*=(Polynomial const& other);

    void operator/=(int const& other);
    void operator/=(Monomial const& other);
    void operator/=(Polynomial const& other);

    void operator=(QString const& value);
    void operator=(const char* value);

private:
    QVector<Monomial> monomeVector;
};

Polynomial operator+(Polynomial const& a, Monomial const& b);
Polynomial operator+(Polynomial const& a, Polynomial const& b);

Polynomial operator-(Polynomial const& a, Monomial const& b);
Polynomial operator-(Polynomial const& a, Polynomial const& b);
Polynomial operator*(Polynomial const& a, int const& b);
Polynomial operator*(int const& b, Polynomial const& a);
Polynomial operator*(Polynomial const& a, int const& b);
Polynomial operator*(Polynomial const& a, Monomial const& b);
Polynomial operator*(Polynomial const& a, Polynomial const& b);

Polynomial operator/(Polynomial const& a, int const& b);
Polynomial operator/(int const& b, Polynomial const& a);
Polynomial operator/(Polynomial const& a, Monomial const& b);
Polynomial operator/(Monomial const& b, Polynomial const& a);
Polynomial operator/(Polynomial const& a, Polynomial const& b);

class PolynomialFraction
{
public:
    PolynomialFraction();
    PolynomialFraction(Polynomial numerator);
    PolynomialFraction(Polynomial numerator, Polynomial denominator);
    PolynomialFraction(QString numerator);
    PolynomialFraction(QString numerator, QString denominator);
    PolynomialFraction(const char* numerator);
    PolynomialFraction(const char* numerator, const char* denominator);

    QString toString() const;

    void invert();

    Polynomial getNumerator() const;
    Polynomial getDenominator() const;

    void setNumerator(Polynomial numerator);
    void setDenominator(Polynomial denominator);

    void operator+=(int const& other);
    void operator+=(Monomial const& other);
    void operator+=(Polynomial const& other);
    void operator+=(PolynomialFraction const& other);

    void operator-=(int const& other);
    void operator-=(Monomial const& other);
    void operator-=(Polynomial const& other);
    void operator-=(PolynomialFraction const& other);

    void operator*=(int const& other);
    void operator*=(Monomial const& other);
    void operator*=(Polynomial const& other);
    void operator*=(PolynomialFraction const& other);

    void operator/=(int const& other);
    void operator/=(Monomial const& other);
    void operator/=(Polynomial const& other);
    void operator/=(PolynomialFraction const& other);

    void operator=(QString const& value);
    void operator=(char* const value);

private:
    Polynomial numerator, denominator;
};

PolynomialFraction operator+(PolynomialFraction const& a, int const& b);
PolynomialFraction operator+(PolynomialFraction const& a, Monomial const& b);
PolynomialFraction operator+(PolynomialFraction const& a, Polynomial const& b);
PolynomialFraction operator+(int const& b, PolynomialFraction const& a);
PolynomialFraction operator+(Monomial const& b, PolynomialFraction const& a);
PolynomialFraction operator+(Polynomial const& b, PolynomialFraction const& a);
PolynomialFraction operator+(PolynomialFraction const& a, PolynomialFraction const& b);

PolynomialFraction operator-(PolynomialFraction const& a, int const& b);
PolynomialFraction operator-(PolynomialFraction const& a, Monomial const& b);
PolynomialFraction operator-(PolynomialFraction const& a, Polynomial const& b);
PolynomialFraction operator-(int const& b, PolynomialFraction const& a);
PolynomialFraction operator-(Monomial const& b, PolynomialFraction const& a);
PolynomialFraction operator-(Polynomial const& b, PolynomialFraction const& a);
PolynomialFraction operator-(PolynomialFraction const& a, PolynomialFraction const& b);

PolynomialFraction operator*(PolynomialFraction const& a, int const& b);
PolynomialFraction operator*(PolynomialFraction const& a, Monomial const& b);
PolynomialFraction operator*(PolynomialFraction const& a, Polynomial const& b);
PolynomialFraction operator*(int const& b, PolynomialFraction const& a);
PolynomialFraction operator*(Monomial const& b, PolynomialFraction const& a);
PolynomialFraction operator*(Polynomial const& b, PolynomialFraction const& a);
PolynomialFraction operator*(PolynomialFraction const& a, PolynomialFraction const& b);

PolynomialFraction operator/(PolynomialFraction const& a, int const& b);
PolynomialFraction operator/(PolynomialFraction const& a, Monomial const& b);
PolynomialFraction operator/(PolynomialFraction const& a, Polynomial const& b);
PolynomialFraction operator/(int const& b, PolynomialFraction const& a);
PolynomialFraction operator/(Monomial const& b, PolynomialFraction const& a);
PolynomialFraction operator/(Polynomial const& b, PolynomialFraction const& a);
PolynomialFraction operator/(PolynomialFraction const& a, PolynomialFraction const& b);

#endif // VARIABLESINCLUDE_H

然后,在所有 .cpp 文件中,我将 #include "polynome.h" 替换为 #include "variablesinclude.h"。这样就解决了问题。

【讨论】:

    猜你喜欢
    • 2011-01-22
    • 2017-05-18
    • 2017-07-05
    • 2019-09-19
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多