Re: [問題] 關於static用法

看板C_and_CPP (C/C++)作者 (粉絲?!)時間11年前 (2014/11/28 21:41), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串2/2 (看更多)
※ 引述《wandering25 (晃晃)》之銘言: 我記得一個 class 的 static 成員, 除了在 *.h file 裡被 declare 之外, 還必須在 *.cpp file 裡被 define。 Card.h private: static string suitDict[4]; Card.cpp string Card::suitDict[4]; 不知道我這樣子說對不對,實在是 N 年沒碰 C++ 了。 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : VC++ : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : 問題(Question): : 大家好 : 我是剛學C++的新手 : 剛學到class的部分 : 剛在寫作業時遇到一個問題一直找不出問題在哪 : 就是我在class的header file裡面定義了一個 : private : : static string suitDict[4] ; : 然後在class的constructor裡面寫 : suitDict[0] = "spade"; : suitDict[1] = "heart"; : suitDict[2] = "diamond"; : suitDict[3] = "club"; : 然後complie時就出現錯誤訊息 : 錯誤訊息 : : 錯誤 2 error LNK2001: 無法解析的外部符號 "private: static class : std::basic_string<char,struct std::char_traits<char>,class : std::allocator<char> > * Card::faceDict" : 但是若我把header file裡面的"static"拿到 : 就可以順利執行 : 想請問static這樣用的問題在哪裡? : 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.160.208.65 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1417182096.A.3CD.html

11/28 21:54, , 1F
11/28 21:54, 1F

11/28 22:31, , 2F
Good,我還沒失憶太嚴重 XD
11/28 22:31, 2F
文章代碼(AID): #1KU7kGFD (C_and_CPP)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
文章代碼(AID): #1KU7kGFD (C_and_CPP)