C++ 错误:“...”未命名类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3961103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: '...' does not name a type
提问by Pieter
I had a working project. After rearranging some code, I tried to recompile my project and then weird things started happening. Have a look at this excerpt from the compiler's output. I'm compiling from Eclipse on Windows using MinGW G++.
我有一个工作项目。重新排列一些代码后,我尝试重新编译我的项目,然后开始发生奇怪的事情。查看编译器输出的这段摘录。我正在使用 MinGW G++ 在 Windows 上从 Eclipse 编译。
**** Build of configuration Debug for project Pract2 ****
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -omove.o ..\move.cpp
In file included from ..\/game.h:11:0,
from ..\/piece.h:10,
from ..\/move.h:10,
from ..\move.cpp:7:
..\/board.h:18:2: error: 'Piece' does not name a type
board.h, line 18:
board.h,第 18 行:
Piece* GetPieceAt(int row, int col) const;
Usually when that happens, I just have to add some inclusions I've forgotten to do. But I have in fact included piece.hat the top of board.h.
通常当这种情况发生时,我只需要添加一些我忘记做的内含物。但实际上我已经在board.h的顶部包含了piece.h。
My second thought was that the compiler must have generated an error somewhere in the Piece
class that would cause the compiler ignore the existence of the class, which in turn would cause more errors. In that case I'd look at the first compiler error, which I expected would be something about an error in piece.cppor piece.h. But the first error isn't about Piece
at all, so I looked if Eclipse had marked any errors in piece.cppor piece.h. Nope, not a red line in sight. I only saw a few of these unexplained yellow markers.
我的第二个想法是编译器一定在Piece
类中的某个地方生成了一个错误,这会导致编译器忽略该类的存在,从而导致更多的错误。在那种情况下,我会查看第一个编译器错误,我预计它会与piece.cpp或piece.h 中的错误有关。但是第一个错误根本不是关于Piece
的,所以我查看了 Eclipse 是否在piece.cpp或piece.h 中标记了任何错误。不,不是视线中的红线。我只看到了一些无法解释的黄色标记。
One last thing that I checked was that every header file contained inclusion guards, which they did.
我检查的最后一件事是每个头文件都包含包含保护,他们确实这样做了。
How do I get to the bottom of this error? I'd post code snippets, but then I would probably end up pasting the entire code (which I can't do) because additional context may be necessary.
我如何找到这个错误的根源?我会发布代码片段,但是我可能最终会粘贴整个代码(我不能这样做),因为可能需要额外的上下文。
Edit: here's board.hup to line 18. (I've left out a big comment, that explains why this block of code is smaller than you'd expect.)
编辑:这里是board.h到第 18 行。(我遗漏了一个很大的注释,这解释了为什么这段代码比你预期的要小。)
#ifndef BOARD_H_
#define BOARD_H_
#include "piece.h"
class Board {
public:
// Prototypes for externally defined functions
Board();
~Board();
void PrintBoard();
Piece* GetPieceAt(int row, int col) const;
回答by AnT
Most likely by "rearranging your code" you created a circular inclusion between board.h
and piece.h
. Your header files contain include guards that prevent infinite inclusion in such cases, but that will not help the declarations to compile.
很可能通过“重新排列您的代码”,您在board.h
和之间创建了一个循环包含piece.h
。您的头文件包含在这种情况下防止无限包含的包含守卫,但这无助于声明的编译。
Check for circular inclusion and rethink you inclusion strategy accordingly. If you don't have any circular dependencies between the types, you should be able to eliminate the circular inclusion easily. If you do have the circular dependency between the types, you'll have to resort to forward declarations for some types. Attempting to include header files in circular fashion will not achieve anything.
检查循环包含并相应地重新考虑您的包含策略。如果类型之间没有任何循环依赖关系,则应该能够轻松消除循环包含。如果类型之间确实存在循环依赖关系,则必须对某些类型使用前向声明。尝试以循环方式包含头文件不会实现任何目标。
回答by MarcoH
It seems that piece.h and board.h are included by each other. If this is the case try to use predeclarations of both classes and include the headers only in the cpp files
似乎piece.h 和board.h 是相互包含的。如果是这种情况,请尝试使用两个类的预声明,并仅在 cpp 文件中包含头文件