2011-12-31 90 views
2

我不知道我在做什麼錯誤,我以爲一切都被定義了一次,一切正確鏈接,但我想這不會是這樣...鏈接錯誤 - 請幫助:錯誤LNK2001:無法解析的外部符號

這裏是我的編譯器給我的錯誤:

1>StaticElements.obj : error LNK2001: unresolved external symbol "private: static int Powerup_Health::g_refCount" ([email protected][email protected]@0HA) 
1>StaticElements.obj : error LNK2001: unresolved external symbol "private: static class DBModel * Powerup_Health::g_pModel" ([email protected][email protected]@[email protected]@A) 
1>StaticElements.obj : error LNK2001: unresolved external symbol "private: static int Powerup_QuadDamage::g_refCount" ([email protected][email protected]@0HA) 
1>StaticElements.obj : error LNK2001: unresolved external symbol "private: static class DBModel * Powerup_QuadDamage::g_pModel" ([email protected][email protected]@[email protected]@A) 

代碼:

StaticElements.h

#pragma once 

#include "D3DUtil.h" 

class ContentManager; 
class Level; 
class GraphCamera; 
class Powerup_Health; 
class Powerup_QuadDamage; 

class StaticElements 
{ 
public: 
    StaticElements(){}; 
    ~StaticElements(){}; 

    void PreLevelInitialisation(Level* pLevel); 
    void PostLevelInitialisation(); 

private: 
    Powerup_Health* m_pPwrHealth; 
    Powerup_QuadDamage* m_pPwrQuadDamage; 
}; 

StaticElements.cpp

#include "StdAfx.h" 

#include "StaticElements.h" 

#include "Level.h" 

#include "DBModel.h" 

#include "Powerup_Health.h" 
#include "Powerup_QuadDamage.h" 


void StaticElements::PreLevelInitialisation(Level* pLevel) 
{ 
//////POWERUPS 
    Powerup_Health::InitModel(); 
    m_pPwrHealth = new Powerup_Health(); 
    pLevel->AddChild(m_pPwrHealth); 

    Powerup_QuadDamage::InitModel(); 
    m_pPwrQuadDamage = new Powerup_QuadDamage(); 
    pLevel->AddChild(m_pPwrQuadDamage); 
} 

Powerup_QuadDamage.h

#pragma once 

#include "Powerup.h" 

class Powerup_QuadDamage : public Powerup 
{ 
private: 
    //static 
    static DBModel* g_pModel; 
    static int g_refCount; 

public: 
    virtual ~Powerup_QuadDamage() { 
     --g_refCount; 
     if (g_refCount == 0) delete g_pModel; 
    } 

    Powerup_QuadDamage() 
     :Powerup(g_pModel, 90.0f) 
    { 
     ++g_refCount; 
    } 

    static void InitModel() { 
     g_refCount = 0; 

     DBModelDesc desc; 
     g_pModel = new DBModel(desc, nullptr); 
    } 

private: 
    //disabled 
    Powerup_QuadDamage(const Powerup_QuadDamage& b); 
    Powerup_QuadDamage& operator= (const Powerup_QuadDamage& b); 
}; 

Powerup_Health.h

#pragma once 

#include "Powerup.h" 

class Powerup_Health : public Powerup 
{ 
private: 
    //static 
    static DBModel* g_pModel; 
    static int g_refCount; 

public: 
    virtual ~Powerup_Health() { 
     --g_refCount; 
     if (g_refCount == 0) delete g_pModel; 
    } 

    Powerup_Health() 
     :Powerup(g_pModel, 20.0f) 
    { 
     ++g_refCount; 
    } 

    static void InitModel() { 
     g_refCount = 0; 

     DBModelDesc desc; 
     g_pModel = new DBModel(desc, nullptr); 
    } 

private: 
    //disabled 
    Powerup_Health(const Powerup_Health& b); 
    Powerup_Health& operator= (const Powerup_Health& b); 
}; 

任何人都可以告訴我是什麼原因導致錯誤,請問如何解決? 謝謝你一堆。

+1

您有'Powerup_Health :: g_refCount'(等),但沒有定義聲明。 – 2011-12-31 03:00:36

+0

@RaymondChen怎麼這樣?請解釋? – xcrypt 2011-12-31 03:02:14

+1

[這個問題的答案](http://stackoverflow.com/questions/806846/query-on-static-member-variables-of-a-class-in-c)解釋。 – 2011-12-31 03:04:11

回答

7

靜態成員變量必須在模塊級別顯式初始化。添加的東西就像在你CPP文件中的以下內容:

int Powerup_Health::g_refCount = 0; 
+0

它也可以在標題中定義? – xcrypt 2011-12-31 03:12:29

+0

通常不是,您希望它只出現在一個編譯單元中。 – 2011-12-31 03:39:44

+0

如果將Powerup_Health包含在許多其他類中,那麼所有這些cpp文件都需要靜態初始化嗎?看起來很痛苦,如果真的... – Nigel 2013-04-29 15:08:30

相關問題