【发布时间】:2013-07-04 12:35:10
【问题描述】:
我对 LNK2001 有问题:未解决的外部符号错误。它仅在我的命名空间中有所有类并引用我使用多个文件的全局变量时才显示。这是我的代码的示例代码:
Engine.h
#pragma once
namespace Engine{
#include "Core.h"
#include "Display.h"
#include "Box.h"
// ... some code...
}
using namespace Engine;
Core.cpp
#include "Engine.h"
// ...some code...
Core.h
extern Matrix matrix;
// ... some code...
Display.cpp
#include "Engine.h"
Matrix matrix;
// ... some code...
Display.h
// ... some code...
Box.cpp
void Box::draw(PxShape *shape){
// matrix=.. some code...
}
Box.h
// ... some code...
错误信息
1>Box.obj : error LNK2001: unresolved external symbol "struct Engine::Matrix Engine::matrix" (?matrix@Engine@@3UMatrix@1@A)
当我评论命名空间时,一切正常。这是我第一次想使用命名空间,但我不知道该怎么做。
【问题讨论】:
-
如果在定义此命名空间的同一个头文件中,将
Engine命名空间的所有元素引入全局命名空间,则绝对没有理由定义命名空间Engine。 -
在定义命名空间之前,我还包含了一些第 3 方库和标准 c++ 头文件,我想表明这些类是由我提供的。
标签: c++ namespaces extern unresolved-external lnk2001