2017-03-13 30 views
1

我有世界級的world.h:無法解析的外部,試圖用靜態變量C++

class World 
{ 
public: 
    static Ground* ground; 
}; 

,並在功能的其他類我試圖用靜態變量地面像:

#include "Node.h" 
#include "World.h" 
void Node::Foo() 
{ 
    Ground* ground = World::ground; 
} 

以及在world.cpp我有:

#include "stdafx.h" 
#include "World.h" 

static Ground* ground = new Ground(10, 10); 

,但我得到了以下錯誤:

  • LNK2001解析的外部符號:(?地面@世界@@ 2PAVGround @@ A) 「公共靜態類地世界::地」
  • LNK1120 1個無法解析外部

image of the errors:

+1

請包括錯誤的文本,而不是圖像 –

+2

'靜態地面*地面=新的地面(10,10);' - >'接地*世界::地面=新的地面(10 ,10);' – George

+1

這個問題可能是重複的,但它的含義比原來的問題要明顯得多。 OP爲+1。 – Dan

回答

2
static Ground* ground = new Ground(10, 10); 

你在那裏缺少World::,所以你定義了一個完全不相關的變量,只是碰巧有相同的名字。你應該有這樣的:

Ground* World::ground = new Ground(10, 10); 
+0

我確實必須添加'World ::',並且我還必須從cpp文件中移除'static'。 –

+1

好點。我編輯了我的答案以反映這一點。 –

相關問題