2011-12-31 106 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

相关问题