[語法] 關於static data member

看板C_and_CPP (C/C++)作者 (一個人)時間16年前 (2009/12/18 18:55), 編輯推噓1(108)
留言9則, 2人參與, 最新討論串1/1
我的程式找不出問題 他出現 1>10.8.obj : error LNK2001: 無法解析的外部符號 "private: static double savingaccount::annualInterestRate" (?annualInterestRate@savingaccount@@0NA) 1>MSVCRTD.lib(crtexe.obj) : error LNK2019: 無法解析的外部符號 _main 在函式 ___tmainCRTStartup 中被參考 1>C:\Users\xxxx\Documents\Visual Studio 2008\Projects\10.8\Debug\10.8.exe : fatal error LNK1120: 2 個無法解析的外部符號 我是要寫一個所有物件都能使用的 static data member 可是我寫了這些程式碼 我先po .h #include <iostream> using namespace std; class savingaccount { friend ostream &operator<<(ostream cout,savingaccount & right); friend istream &operator>>(istream cin ,savingaccount & right); public: savingaccount(int =0); double calculatemonthlyinterest(void); static void modifyinterestrate(double); void savingsBalancecall(int); double getbalance(void); private: static double annualInterestRate; double savingsBalance; double interest; }; 然後cpp的地方 我是寫 #include <iostream> #include "10.8.h" using namespace std; double savingaccount::calculatemonthlyinterest(void) { interest=savingsBalance*annualInterestRate/12; return interest; } void savingaccount::modifyinterestrate(double c) { annualInterestRate=c; } ostream &operator<<(ostream & cout,savingaccount & right) { cout<<right.getbalance(); return cout; } istream &operator>>(istream & cin ,savingaccount & right) { int a; cin>>a; right.savingsBalancecall(a); return cin; } void savingaccount::savingsBalancecall(int b) { savingsBalance=b; } double savingaccount::getbalance(void) { return savingsBalance; } 他出現一開始出現的問題 我不知道該怎麼解決 有人可以救我嗎.....我不知道問題出在哪 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.4.234

12/18 19:03, , 1F
在main上面多寫一行定義:
12/18 19:03, 1F

12/18 19:03, , 2F
double savingaccount::annualInterestRate;
12/18 19:03, 2F

12/18 19:03, , 3F
也可以順便給初值.
12/18 19:03, 3F

12/18 19:05, , 4F
理由在此 - http://0rz.tw/HuwOA // MSDN
12/18 19:05, 4F

12/18 22:53, , 5F
咦?有某個論譠也有人問savingaccount作業耶~
12/18 22:53, 5F

12/18 22:54, , 6F
而且連函式名稱都一模一樣~︿_︿|||
12/18 22:54, 6F

12/18 22:57, , 7F
是不是有兩個帳戶?各為兩千與三千,年利率三趴與四趴?
12/18 22:57, 7F

12/18 23:01, , 8F

12/18 23:02, , 9F
上面那個網址是那個人問的作業,題目應該是一模一樣。
12/18 23:02, 9F
文章代碼(AID): #1BAr-FnO (C_and_CPP)
文章代碼(AID): #1BAr-FnO (C_and_CPP)