2013-07-27 44 views
0

我在Visual Studio中爲我的控制檯應用程序創建了一個DLL。 在我的DLL中,我有一個名爲Dialog_MainMenu的類,它有一個* .cpp文件和一個* .h文件。無法解析的外部符號_declspec(dllimport)

以下錯誤消息

Error 9 error LNK2001: unresolved external symbol "__declspec(dllimport) public: static enum Dialog_MainMenu::GAME_STATES Dialog_MainMenu::CurrentGameState" (_imp[email protected][email protected]@[email protected]@A) C:\Users\Kevin\Desktop\c++ projects\development_testing\The Intense Adventure\Dialogs\Dialog_MainMenu.obj Dialogs

對此我有點不明白。這隻發生在我的頭文件中將枚舉添加到我的原型時。

頭文件

#ifdef DIALOG_MAINMENU_EXPORTS 
#define DIALOG_MAINMENU_API __declspec(dllexport) 
#else 
#define DIALOG_MAINMENU_API __declspec(dllimport) 
#endif 

class Dialog_MainMenu { 
public: 
    static DIALOG_MAINMENU_API enum GAME_STATES { 
     MAINMENU, GAME, OPTIONS, CREDITS, QUIT 
    }; 
    static DIALOG_MAINMENU_API GAME_STATES CurrentGameState; 
    DIALOG_MAINMENU_API GAME_STATES GetState(); 
}; 

(不知道是不是問題就出在這裏,所以我就加吧)一般 CPP文件:

//Get state 
Dialog_MainMenu::GAME_STATES Dialog_MainMenu::GetState() { 
// Code.. 
} 

//Switching state 
Dialog_MainMenu::CurrentGameState = Dialog_MainMenu::GAME_STATES::GAME; 

我真的很感激,任何幫助或至少一些建議,我可以在這裏瞭解更多關於這個問題。

+0

您是否引用.lib文件? –

+0

我確實做到了。我甚至三重檢查。 –

+0

你可以添加用於你的問題的鏈接命令嗎? – greatwolf

回答

3

您需要在全局範圍的cpp文件中定義靜態成員。

Dialog_MainMenu::GAME_STATES Dialog_MainMenu::CurrentGameState; 

或者,您也可以爲其分配一些初始值。

Dialog_MainMenu::GAME_STATES Dialog_MainMenu::CurrentGameState = Dialog_MainMenu::GAME_STATES::GAME; 

編輯

I've created an DLL for my Console Application in Visual Studio. In my DLL I have a Class named Dialog_MainMenu with has a *.cpp file and a *.h file.

確定 - 當你編譯DLL - 要導出的類型。所以,你需要define dll的.cpp文件中的靜態成員。您還需要確保在編譯器設置中啓用了DIALOG_MAINMENU_EXPORTS的定義。這將確保類型被導出。

現在,當您將控制檯應用程序與dll鏈接時 - 您將在編譯器設置中啓用DIALOG_MAINMENU_EXPORTS的任何定義(只保留設置默認值)#include dll的標題。這將使編譯器明白,現在您正在將dll中的類型導入到控制檯應用程序中。

我希望現在清楚。

+0

那是哪裏?似乎無法找到的地方,在哪裏我把你輸入的第一行以下行。 –

+0

就像你在'.h'文件中聲明'方法並在'.cpp'中'定義'它們一樣 - 你還需要'定義'靜態成員。你可以把上面的代碼行放在Dialog_MainMenu :: GAME_STATES Dialog_MainMenu :: GetState(){' – YK1

+0

這只是給我一個錯誤:定義dllimport靜態數據成員不允許 –

1

沒有與出口靜態類成員一個問題:

If you declare a static data member within a class definition as dllexport, a definition must occur somewhere within the same program (as with nonclass external linkage).

但我通常做的是使用的訪問方法是什麼。靜態函數方法鏈接正常。

//.h file 
class Dialog_MainMenu { 
public: 
    static DIALOG_MAINMENU_API enum GAME_STATES { 
     MAINMENU, GAME, OPTIONS, CREDITS, QUIT 
    }; 
    static GAME_STATES CurrentGameState; 
    DIALOG_MAINMENU_API GAME_STATES GetState(); 

    static DIALOG_MAINMENU_API GAME_STATES& GetCurrentState(); 
}; 

//.cpp file 

GAME_STATES& Dialog_MainMenu ::GetCurrentState() 
{ 

return CurrentGameState; 
}