我有一個結構,我想填寫一個單獨的源文件從我正在運行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'」。我在答覆的評論中也提到了這一點。 –