c++ - 命名空间之前的预期初始化器

标签 c++ sdl initializer

因此,我是 C++ 编程的新手,但我已将 SDL 与 python 和 FreeBASIC 广泛结合使用。我确定我在这里遗漏了一些愚蠢的东西,但无论我尝试什么,我的 video.h 文件中都会不断收到错误“错误:'命名空间'之前的预期初始化程序”。这让我有点发疯。

#include "SDL/SDL.h"
#include <iostream>

namespace video {
// This is here because like video, everything uses it and the players should never be  able to touch it.
int rolldice(int minimumroll, int maximumroll, int numberofdice);
// Same Here.
char* charraystring(std::string prestring);
// Now we're in video proper
// This function loads an image, checks to make sure it works, returns the image, and unloads the testing surface.
SDL_Surface* loadimage(std::string path);
// This is an optimized blitter that will exit with a signal if it encounters an error.
void oblit(SDL_Surface* pic, SDL_Rect frame, SDL_Surface* screen, SDL_Rect location);
}

最佳答案

您提供的错误 error: expected initializer before ‘namespace’ 表明存在未终止的结构或变量声明。像这样的东西:

struct foo {
    ...
}

namespace video {
    ...

在这里,'struct foo' 声明没有以分号结束。这应该是:

struct foo {
    ...
};

namespace video {
    ...

让预处理器参与进来(使用#include)使得这类事情更难追踪。例如,您可能包含了一个不终止结构定义的 header (就在 namespace video 声明之前)。

去检查你所有的 structs 和 classes 在你的头文件和源文件中的右大括号之后有一个分号。同样,任何变量声明,例如

int value // <-- oops, forgot the ';'

namespace video {
    ...

关于c++ - 命名空间之前的预期初始化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9103454/

相关文章:

c++ - 初始化一个容器,它是一个类的私有(private)成员变量

c++ - 具有嵌套初始化列表的子对象 std::array 的聚合初始化

c++ - 关于const或friendship的使用

c++ - 创建DirectX 10缓冲区时出现内存泄漏

c++ - session 0 捕获屏幕

c++ - 纹理渲染和 VBO 的 [OpenGL/SDL/C++]

c++ - 使代码更小以实现多重继承

c++ - 这个用于 SDL 类型的智能指针包装器安全吗?

c++ - 在c++中绘制抗锯齿圆角矩形

ios - 使用可变参数调用 Objective-C 初始化器