2013-10-15 129 views
0

我需要創建字符串變量string time,它應該看起來像14:58
我創建功能C++錯誤LNK2019

string SetTime() { 
long double h = (long double)(rand()%25); 
long double m = (long double)(rand()%60); 

string hour = to_string(h); 
string minutes = (m <= 9 ? "0" : "") + to_string(m); 

string time = hour + ":" + minutes; 
return time; 
} 

,但是當我嘗試使用它

string str = SetNumber(); 
cout << str; 

我得到
error LNK2019: link to unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl SetNumber(void)" ([email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XZ) in _wmain

什麼問題,如何解決?

+0

你在調用SetNumber/SetTime函數嗎? – billz

+0

是的,我混淆了兩個功能,對不起。 – Heidel

回答

3

你的函數被調用SetTime當你調用SetNumber。鏈接器找不到SetNumber的定義。有趣的是,你得到一個鏈接器錯誤,而不是編譯器錯誤。這意味着你已經宣佈SetNumber

+0

哦,對不起,這麼愚蠢的錯誤,我試圖用其他我的功能。 – Heidel

+0

有一個亂七八糟的增量構建它可能會發生,編譯器(本例中的VC++)認爲它不需要重新編譯被調用的代碼,然後鏈接器不能將調用者代碼鏈接到舊代碼。完全重建應該可以解決這個問題。 – TemplateRex

1

你應該叫SETTIME,不SetNumber

+0

哦,對不起,這麼愚蠢的錯誤,我試圖用其他我的功能。 – Heidel