我对 C++ 命名空间以及您如何定义它们有点困惑。我有两个文件:Lua.h
和Main.cpp
. Lua.h
包含以下帮助程序,用于在命名空间中运行 Lua 脚本:
#ifndef Lua_h
#define Lua_h
#include <lua.hpp>
namespace fabric
{
namespace lua
{
void loadLibs(lua_State * L)
{
static const luaL_Reg luaLibs[] =
{
{ "io", luaopen_io },
{ "base", luaopen_base },
{ NULL, NULL }
};
const luaL_Reg * lib = luaLibs;
for (; lib->func != NULL; lib++)
{
lib->func(L);
lua_settop(L, 0);
}
}
void init(lua_State * L)
{
loadLibs(L);
luaL_dofile(L, "Init.lua");
lua_close(L);
}
}
}
#endif
我的Main.cpp
文件尝试使用这些辅助函数运行 Lua 脚本:
#include "Lua.h"
int main (int argc, char * argv[])
{
fabric::lua::init();
return 0;
}
但是当我尝试编译时Main.cpp
,我得到了这个:
Source/Main.cpp:9:3: error: use of undeclared identifier 'fabric'
fabric::lua::init();
^
我只是对如何定义这个命名空间感到困惑。辅助函数的代码都很好,但Main.cpp
找不到命名空间。谁能给我一些关于如何在 C++ 中正确定义这个命名空间的指示?
编辑:
现在工作。出于某种原因,我的-I
标志无法编译,因为我的标题位于Include/
. 我也改名Lua.h
为LuaHelpers.h
.