错误:一个声明中有多种类型
我将 Code::Blocks 和 Mingw32 与 SDL 库一起使用。该错误出现在我的代码的第 13 行(下面有注释)。
经过一番搜索后,我认为这是缺少分号(;),但这里的情况似乎并非如此。其他研究表明,这可能是包含文件中的错误。
不幸的是,包含中没有错误,即使注释掉了包含,此错误仍然存在。当枚举块被注释掉时,错误会跳转到类声明的末尾。
#ifndef _TILE_H_
#define _TILE_H_
#include "Define.h"
enum
{
TILE_TYPE_NONE = 0,
TILE_TYPE_GROUND,
TILE_TYPE_RAMPUP,
TILE_TYPE_RAISED,
TILE_TYPE_RAMPDOWN
}; //error occurs here (line 13)
class Tile
{
public:
int TileID;
int TypeID;
public:
Tile();
};
#endif
这实际上是在添加一个新类之后开始发生的,但是新类完全不相关,并且根本不使用、包含或继承已发布的类。
任何建议或信息将不胜感激。
编辑(添加 Define.h):
#ifndef _DEFINE_H_
#define _DEFINE_H_
#define MAP_WIDTH 40
#define MAP_HEIGHT 40
#define TILE_SIZE 16
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#endif
I am using Code::Blocks and Mingw32 with the SDL libraries. The error appears at line 13 of my code (commented below).
After some searching I believed it to be a missing semicolon(;), this doesn't appear to be the case here though. Additional research threw up the fact that it may be an error in an include file.
Unfortunately there are no errors in the includes and even when the include is commented out this error persists. When the enum block is commented out the error jumps to the end of the class declaration.
#ifndef _TILE_H_
#define _TILE_H_
#include "Define.h"
enum
{
TILE_TYPE_NONE = 0,
TILE_TYPE_GROUND,
TILE_TYPE_RAMPUP,
TILE_TYPE_RAISED,
TILE_TYPE_RAMPDOWN
}; //error occurs here (line 13)
class Tile
{
public:
int TileID;
int TypeID;
public:
Tile();
};
#endif
This actually started happening after adding a new class, however the new class is completely unrelated and does not use, include or inherit from the posted one at all.
Any advice or information would be really appreciated.
EDIT (adding Define.h):
#ifndef _DEFINE_H_
#define _DEFINE_H_
#define MAP_WIDTH 40
#define MAP_HEIGHT 40
#define TILE_SIZE 16
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有一个包含以下内容的文件:
在
Something.h
中,您有以下内容:您缺少分号,因此编译器会看到:
这是一个包含两种类型的声明(一个
class
和enum
),这是不允许的。class
、struct
和enum
之后需要使用分号,因为您可以在与类型相同的声明中声明新类型的实例:(此外,以
_
和大写字母开头的名称将被保留,请使用TILE_H
而不是_TILE_H_
。)You have a file with this:
In
Something.h
, you have this:You are missing a semicolon, so the compiler sees:
Which is one declaration containing two types (a
class
and anenum
), which is not allowed. The semicolon is required afterclass
,struct
, andenum
because you can declare instances of a new type in the same declaration as the type:(Also, names starting with
_
and a capital letter are reserved. UseTILE_H
instead of_TILE_H_
.)当我注释掉 Define.h 的包含内容时,使用 g++ 可以很好地编译该代码。正如您所建议的,这表明包含的文件中确实可能存在问题。也许您在 Define.h 末尾的某个地方缺少分号,或者那里存在其他语法问题。
When I comment out the include for Define.h, this code compiles fine for me using g++. That suggests that there may indeed be a problem in the included file, as you suggest. Perhaps you're missing a semicolon somewhere at the end of Define.h, or there is some other syntax problem there.
我将您的第一个文件命名为
Tile.h
并创建了Tile.cpp
,如下所示:然后我使用 g++ 在 Ubuntu Linux 上进行编译:
我既没有收到编译错误,也没有收到运行时错误(与
gcc
相同)。您的问题出在其他地方 - 要么是在您未向我们展示的#include
d 文件中,要么是某种特定于平台的命名冲突。我会尝试逐步重命名所有内容,直到问题消失。编辑:在这种情况下,您的原始代码未更改。
I called your first file
Tile.h
and createdTile.cpp
as follows:Then I compiled on Ubuntu Linux with g++:
I received neither a compilation nor runtime error (same for
gcc
). Your problem is somewhere else--either in#include
d files you're not showing us, or some kind of platform-specific naming conflict. I'd try progressively renaming everything until the problem goes away.Edit: Your original code unaltered in this case.