为什么这个错误如此不清楚
Why is this error so unclear?
我有以下代码:
福.H
#ifndef FOO_H
#define FOO_H
#include "bar.h"
class Foo
{
public:
Foo(Bar bar);
};
#endif //FOO_H
酒吧.H
#ifndef BAR_H
#define BAR_H
#include "foo.h"
class Bar
{
public:
Bar(Foo foo);
};
#endif //BAR_H
如果我编译它,我会收到以下错误消息:
expected ')' before 'foo' bar.h line 9
在查看此网站后,我通过在 bar.h
中使用 Foo
的前向声明来修复它,并在 foo.h
中Bar
。
我的问题是,为什么编译器使此错误听起来像语法错误,而实际上并非如此?我认为捕获此类错误并返回正确的错误消息将非常简单。
您有未解析的循环依赖项的标头。也就是说,当您的代码在某处首先包含"foo.h",然后在预处理后它将变成
class Bar // expanded from #include "bar.h"
{
public:
Bar(Foo foo); // Foo is not declared at this point
};
class Foo // rest of foo.h content
{
public:
Foo(Bar bar);
};
如果你的代码首先包含"bar.h",那么经过预处理后,它将变成
class Foo // expanded from #include "foo.h"
{
public:
Foo(Bar bar); // Bar is not declared at this point
};
class Bar // rest of bar.h content
{
public:
Bar(Foo foo);
};
所以这两种情况都有错误。
要解决此问题,您需要使用适当的前向声明:
// foo.fwd.h
#ifndef FOO_FWD_H
#define FOO_FWD_H
class Foo;
#endif // FOO_FWD_H
// bar.fwd.h
#ifndef BAR_FWD_H
#define BAR_FWD_H
class Bar;
#endif // BAR_FWD_H
并将它们包含在标头中,而不是具有完整类声明的标头中:
// foo.h
#ifndef FOO_H
#define FOO_H
#include "bar.fwd.h"
class Foo
{
public:
Foo(Bar bar);
};
#endif //FOO_H
// bar.h
#ifndef BAR_H
#define BAR_H
#include "foo.fwd.h"
class Bar
{
public:
Bar(Foo foo);
};
#endif //BAR_H
然后仅将带有类定义的标头包含在.cpp或实现文件中。
>C++很难解析。当编译器不知道Foo
是某种类型的名称时,它希望我们尝试在 Bar
中声明具有该名称的成员。代码不会解析为成员声明的任何有效变体。
旧的编译器只是诊断出"语法错误"等情况。现代编译器试图变得更友好。诊断可能会试图帮助我们更正此类(或其他类似(有效成员声明之一的代码。
class Bar
{
public:
Bar (Foo());
Bar (*Moo);
Bar Roo();
};
不幸的是,它完全猜错了,因为Foo
不是作为成员名称,而是作为构造函数的一种参数。
相关文章:
- 我在c ++矩阵计算器程序中找不到错误
- 关于隐式声明的复制构造函数的引用在逻辑上不清楚
- C++复制 c'tor 现在确实会采取行动。 不清楚为什么
- 我在代码中找不到错误以找到最大的素因数?
- 如何摆脱C++中的枚举不匹配错误?
- 在我的应用逻辑中找不到错误
- 内建数组出现不允许的类型不完整错误
- 矢量编程:不清楚矢量的初始化
- 为什么 someNumber = rand() & 100 + 1;不产生错误?
- 在模板类中找不到错误
- C++ 模板不明确错误
- 为什么链接器在下面的代码中不发出错误?
- 调用虚函数的逻辑不清楚(或者是方法隐藏?
- cudaMemcpy 在从设备读取到主机时返回 cudaErrorInvalidArgument,不清楚原因
- 实现附加对象:不清楚的文档示例
- OpenGL 着色器程序无法验证,但不提供错误消息
- 我的程序运行良好,可以复制对象,但是当我使用复制分配(=)时,它仍然可以正常运行.为什么不给错误
- 为什么这个错误如此不清楚
- Visual Studio 2012 中的错误日志不清楚
- 使用Boost Coroutine(1.55)的不清楚分割错误