简体   繁体   English

错误:'...'没有命名类型

[英]Error: '…' does not name a type

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.h at 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. 我的第二个想法是编译器必须在Piece类中的某处产生一个错误,这会导致编译器忽略类的存在,这反过来会导致更多的错误。 In that case I'd look at the first compiler error, which I expected would be something about an error in piece.cpp or piece.h . 在那种情况下,我会查看第一个编译器错误,我预计这将是关于piece.cpppiece.h中的错误。 But the first error isn't about Piece at all, so I looked if Eclipse had marked any errors in piece.cpp or piece.h . 但是第一个错误根本不是关于Piece ,所以我看看Eclipse是否在piece.cpppiece.h中标记了任何错误。 Nope, not a red line in sight. 不,看不到红线。 I only saw a few of these unexplained yellow markers . 我只看到了一些这些无法解释的黄色标记

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.h up 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;

Most likely by "rearranging your code" you created a circular inclusion between board.h and piece.h . 最有可能通过“重新排列代码”,在board.hpiece.h之间创建了一个循环包含。 Your header files contain include guards that prevent infinite inclusion in such cases, but that will not help the declarations to compile. 您的头文件包含防止在这种情况下无限包含的包含保护,但这无助于编译声明。

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. 试图以循环方式包含头文件将无法实现任何目标。

It seems that piece.h and board.h are included by each other. 似乎piece.h和board.h是相互包含的。 If this is the case try to use predeclarations of both classes and include the headers only in the cpp files 如果是这种情况,请尝试使用两个类的预先声明,并仅在cpp文件中包含标头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM