我有一个结构,我想填写一个单独的源文件从我正在运行main。在头文件中我包含了extern,但是当我去定义源文件中的变量时,它不起作用。Extern变量C++
//This is a header file
struct example {
int data1;
float data2;
bool example;
}
extern example tmp;
,然后在源文件:
example *tmp = new example;
我得到错误 “Expected unqualified-id
”。这是为什么?
你真的环顾四周吗? http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c在这里回答它很好。另外我没有明白:为什么你使用不同的数据类型?(ptr和var) – Lasoloz
我真的环顾四周,已经看到了。然而,这并没有帮助这个特定的例子。我正在使用ptr,因为当我没有收到错误消息:“没有可行的从'example *'转换为'example'”。我在答复的评论中也提到了这一点。 –