【发布时间】: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++