2016-07-22 67 views
0

我想實現一個vitual鍵盤類,因爲我有不同的鍵盤佈局如圖所示。LNK 2001:無法解析的外部符號,同時初始化靜態成員

KeyBoard.hpp

class CKeyBoard 
{ 
    public: 

    enum eKeyBoardLayout 
    { 
     UPPER_CASE_ALPHABETS, 
     LOWER_CASE_ALPHABETS, 
     UPPER_CASE_NUMBERS, 
     LOWER_CASE_NUMBERS, 
     MAX_KEYBOARDLAYOUTS, 
    }; 
    CKeyBoard(); 
    void hideCharacters(bool hide); // for password 
    void setDisplayBufferLength(UNSIGNED8_T maxCharToDisplay); 
    virtual ~CKeyBoard(); 
    static const UNSIGNED8_T const_noOfRows = 0x03; // 3 rows for character keys 
    struct SLayout 
    { 
     UNSIGNED8_T noOfColumns[const_noOfRows]; ///< noofcolumns in each row may be different 
     TypedText  textAreaFont; 
     const KeyMapping* pKeyMapping; 
    }; 
    static const SLayout m_KeyBoardLayouts[MAX_KEYBOARDLAYOUTS]; 
private: 
    /*methods*/ 
    void setKeyBoardLayout(eKeyBoardLayout); 
    void backspacePressedHndlr(); 
    void letterCasePressedHndlr(); 
    void alphabetNumberToggleHndlr(); 
    /*members*/ 
    static const UNSIGNED8_T const_maxNoOfKeysSupprtd = 26; 
    CKey m_keyArray[const_maxNoOfKeysSupprtd]; 
    CMenuItem m_KeySpaceBar; 
    CMenuToggle m_KeyCaseSwitch; 
    CMenuToggle m_KeyAlphaNum; 
    CMenuItem m_keyBackSpace; 
    eKeyBoardLayout m_eCurrentKeyLayout; 
    CKeyBoardDisplayArea m_DisplayArea; 

};

我正在初始化靜態成員如下在cpp文件中。 KeyBoard.cpp

static const CKeyBoard::SLayout m_KeyBoardLayouts[CKeyBoard::MAX_KEYBOARDLAYOUTS] = \ 
    { 
     //  UPPER_CASE_ALPHABETS, 
    { 
     { 10, 9, 7 }, 
     TypedText(T_KEYBOARD), 
     &keyMappingsAlphaUpper[0], 

}, 
//  LOWER_CASE_ALPHABETS, 
{ 
    { 10, 9, 7 }, 
    TypedText(T_KEYBOARD), 
    &keyMappingsAlphaLower[0], 
}, 
//  UPPER_CASE_NUMBERS, 
{ 
    { 10, 10, 5 }, 
    TypedText(T_KEYBOARD), 
    &keyMappingsNumLower[0], 
}, 
//  LOWER_CASE_NUMBERS, 
{ 
    { 10, 10, 5 }, 
    TypedText(T_KEYBOARD), 
    &keyMappingsNumLower[0] 
}, 


}; 
// Other function definitions 
CKey::CKey() : m_pCallback(NULL) 
{ 
} 

不過還是我越來越無法解析的外部符號作爲連接錯誤。 我在這裏做錯了什麼?

錯誤詳細信息: * 錯誤LNK2001:無法解析的外部符號 「公用:靜態結構CKeyBoard :: SLayout常量* const的CKeyBoard :: m_KeyBoardLayouts」(m_KeyBoardLayouts @ CKeyBoard @@ 2QBUSLayout @ 1 @ B') *

回答

2

你忘了添加的類別範圍:

static const CKeyBoard::SLayout 
    CKeyBoard::m_KeyBoardLayouts[CKeyBoard::MAX_KEYBOARDLAYOUTS] 

它實際上是一個遺憾,編譯器不會發出警告這樣的靜態鏈接...這是一個常見的錯誤。

+0

哦..感謝您指出這一點.. 但是,當我編輯另一個奇怪的錯誤彈出。 錯誤C2720:'CKeyBoard :: m_KeyBoardLayouts':'static'存儲類說明符在成員上是非法的\t keyboard.cpp \t 並且它通過初始化如下解決。 CKeyBoard :: SLayout常量CKeyBoard :: m_KeyBoardLayouts [CKeyBoard :: MAX_KEYBOARDLAYOUTS] – kernel

+0

爲什麼會發生這種情況? (我知道,我做的初始化是錯誤的) – kernel

+0

這就是C++ :)。你_declare_它'靜態',但你只是_define_符號。 (您不需要在定義之前添加'public',是嗎?請閱讀http://en.cppreference.com/w/cpp/language/static – xtofl