C++ C++#是否仅包含加载一个命名空间?
也许这是个愚蠢的问题,但不知为什么我不能让它起作用。我对C++很陌生,所以我想我可能在某些地方有误解。 graphics.hpp:C++ C++#是否仅包含加载一个命名空间?,c++,xcode,namespaces,C++,Xcode,Namespaces,也许这是个愚蠢的问题,但不知为什么我不能让它起作用。我对C++很陌生,所以我想我可能在某些地方有误解。 graphics.hpp: #ifndef Airport_game_hpp #define Airport_game_hpp #include <SDL2/SDL.h> namespace graphics { namespace gl{ static SDL_Window *mainWindow; static SDL_GLCont
#ifndef Airport_game_hpp
#define Airport_game_hpp
#include <SDL2/SDL.h>
namespace graphics {
namespace gl{
static SDL_Window *mainWindow;
static SDL_GLContext mainContext;
bool initGL();
bool destroyGL();
}
}
#endif
main.cpp:
#include <iostream>
#include "graphics/graphics.hpp"
#include "game/game.hpp"
int main(int argc, char* argv[])
{
std::cout << "Starting application \n";
if (!graphics::gl::initGL()){
std::cout << "OpenGL initialization failed \n";
return false;
}
//Test
game::render(); //This line says: Use of undeclared identifier "game"
graphics::gl::destroyGL();
std::cout << "Exit successful \n";
}
#包括
#包括“graphics/graphics.hpp”
#包括“game/game.hpp”
int main(int argc,char*argv[])
{
std::cout您在两个标头中使用相同的包含卫士。理想情况下,包含卫士应反映模块或标头的名称,但您至少应更改其中一个,以使它们都唯一。两个标头使用相同的包含卫士:Airport\u game\u hpp
。这意味着要包含的第二个标头将被忽略,因为其防护已由第一个定义
将graphics.hpp
的守卫更改为Airport\u graphics\u hpp
,一般来说,请确保你的守卫名称是唯一的。对不起,我弄错了,更新了问题:)你的“graphics”标题有一个包含守卫,看起来它属于“游戏”标题。真正的标题是否也使用机场\u游戏\u hpp
作为守卫,即使张贴的代码没有?是的,抱歉,刚刚意识到。这就是油炸大脑对你所做的!谢谢!
#include <iostream>
#include "graphics/graphics.hpp"
#include "game/game.hpp"
int main(int argc, char* argv[])
{
std::cout << "Starting application \n";
if (!graphics::gl::initGL()){
std::cout << "OpenGL initialization failed \n";
return false;
}
//Test
game::render(); //This line says: Use of undeclared identifier "game"
graphics::gl::destroyGL();
std::cout << "Exit successful \n";
}