我有一个.h
文件,其中包含类定义和静态变量的extern
定义
随附的.cpp
文件声明并初始化此变量。
但是,当我尝试在main.cpp
中访问此变量时,出现does not name a type
错误。
我在做什么错了?
test.h
文件的内容:
#pragma once
namespace Mine {
class Test {
...
};
} // namespace
extern Mine::Test* variable;
test.cpp
文件的内容:
Mine::Test* variable = nullptr;
main.cpp
的内容:
#include "test.h"
variable = new Mine::Test();
答案 0 :(得分:0)
如@ pete-becker所建议的,将variable
中的main.cpp
的初始化放入函数中即可解决此问题。
但是我不明白为什么
[编辑]现在,请参见下面的评论