【问题标题】:Expected initializer before namespace命名空间之前的预期初始化程序
【发布时间】:2012-02-02 05:26:06
【问题描述】:

所以,我对 C++ 编程还很陌生,但我已经在 python 和 FreeBASIC 中广泛使用了 SDL。我确定我在这里遗漏了一些愚蠢的东西,但无论我尝试什么,我都会在我的 video.h 文件中收到错误“错误:'namespace'之前的预期初始化程序”。这让我有点发疯。

#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);
}

【问题讨论】:

  • 第 4 行。实际的错误消息是 /home/dyngar/Workspace/C/CLAIR/video.h:4:1: error: expected initializer before 'namespace' 抱歉,我从文件的旧版本。
  • 你确定这是你的整个文件吗?该错误中的行号不匹配
  • 抱歉,我从文件的早期版本中粘贴了错误消息。它实际上在第 4 行。该死的,你们很快。提前致谢。
  • 您能否发布包含 video.h 的文件的开头以及编译器的完整输出?
  • 如果你通过预处理器运行你的源文件(例如gcc -I /usr/include -I /usr/include/c++/* -E source.cc),“namespace video {”之前的行是什么?

标签: c++ sdl initializer


【解决方案1】:

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

struct foo {
    ...
}

namespace video {
    ...

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

struct foo {
    ...
};

namespace video {
    ...

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

去检查你所有的structs 和classes 在你的头文件和源文件中的右花括号后面都有一个分号。类似地任何变量声明,例如

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

namespace video {
    ...

【讨论】:

  • 我一开始也是这么想的,但是我找不到。我将通过 SDL/SDL.h 文件查看它的内容。我不知道它是否相关,但是当我尝试使用 g++ -I /home/dyngar/Workspace/C/CLAIR -std=gnu++0x video.cpp -o video.o -lSDL 进行编译时,它工作得很好.这看起来很奇怪,但就像我说的,我是 C++ 新手。
猜你喜欢
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多