错误:一个声明中有多种类型

发布于 2025-01-03 06:04:08 字数 813 浏览 1 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦回旧景 2025-01-10 06:04:08

您有一个包含以下内容的文件:

#include "Something.h"
#include "Tile.h"

Something.h 中,您有以下内容:

class Something {
    // ...
}

您缺少分号,因此编译器会看到:

class Something {
    // ...
}
enum {
};

这是一个包含两种类型的声明(一个 classenum),这是不允许的。 classstructenum 之后需要使用分号,因为您可以在与类型相同的声明中声明新类型的实例

struct Point { int x; int y; } my_point;

:(此外,以 _ 和大写字母开头的名称将被保留,请使用 TILE_H 而不是 _TILE_H_。)

You have a file with this:

#include "Something.h"
#include "Tile.h"

In Something.h, you have this:

class Something {
    // ...
}

You are missing a semicolon, so the compiler sees:

class Something {
    // ...
}
enum {
};

Which is one declaration containing two types (a class and an enum), which is not allowed. The semicolon is required after class, struct, and enum because you can declare instances of a new type in the same declaration as the type:

struct Point { int x; int y; } my_point;

(Also, names starting with _ and a capital letter are reserved. Use TILE_H instead of _TILE_H_.)

会傲 2025-01-10 06:04:08

当我注释掉 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.

知足的幸福 2025-01-10 06:04:08

我将您的第一个文件命名为 Tile.h 并创建了 Tile.cpp,如下所示:

#include "Tile.h"

Tile::Tile()
{
}

int main()
{
    return 0;
}

然后我使用 g++ 在 Ubuntu Linux 上进行编译:

$ g++ Tile.cpp -o Tile

我既没有收到编译错误,也没有收到运行时错误(与 gcc 相同)。您的问题出在其他地方 - 要么是在您未向我们展示的 #included 文件中,要么是某种特定于平台的命名冲突。我会尝试逐步重命名所有内容,直到问题消失。

编辑:在这种情况下,您的原始代码未更改。

I called your first file Tile.h and created Tile.cpp as follows:

#include "Tile.h"

Tile::Tile()
{
}

int main()
{
    return 0;
}

Then I compiled on Ubuntu Linux with g++:

$ g++ Tile.cpp -o Tile

I received neither a compilation nor runtime error (same for gcc). Your problem is somewhere else--either in #included 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文