一、C++ 錯(cuò)誤類型 :error LNK2019 無(wú)法解析的外部符號(hào)
注:本人用的編譯器版本:VS2015
C++中報(bào) error LNK2019、LNK2001、......等類型的錯(cuò)誤,對(duì)剛接觸C++的同學(xué)來(lái)說(shuō)是一件非常令人頭疼的事情,因?yàn)樵斐纱祟悊?wèn)題的原因非常多,這里只講本人報(bào)錯(cuò)的一種原因。
二、報(bào)錯(cuò)截圖

50.jpg

51.jpg
三、報(bào)錯(cuò)原因
1、本人是因?yàn)楹瘮?shù)聲明 accumulate 與函數(shù)實(shí)現(xiàn)名 **acculate **稱不一致導(dǎo)致報(bào) error LNK2019 錯(cuò)。
2、在聲明函數(shù) accumulate(......) 的時(shí)候,未將該函數(shù)實(shí)現(xiàn),導(dǎo)致報(bào)錯(cuò),這是造成 error LNK2019 錯(cuò)誤原因的一種;
報(bào)錯(cuò)前的代碼:
1 free_throws& accumulate(free_throws& target, const free_throws& source); // 函數(shù)聲明
2
3 int main() // 主函數(shù)
4 {
5 ...........
6 }
7
8 free_throws& acculate(free_throws & target, const free_throws & source) // 函數(shù)實(shí)現(xiàn)
9 {
10 target.attempts += source.attempts;
11 target.made += source.made;
12 set_pc(target);
13 return target;
14 }
四、解決辦法
1.首先檢查所聲明的所有函數(shù)都有沒(méi)有實(shí)現(xiàn);
2.檢查聲明函數(shù)與實(shí)現(xiàn)函數(shù)的函數(shù)名、參數(shù)列表、返回值是否一致;
3.檢查后期自己是否有修改過(guò)函數(shù)名,導(dǎo)致聲明函數(shù)名與實(shí)現(xiàn)函數(shù)名不一致
修改后的代碼:
1 free_throws& accumulate(free_throws& target, const free_throws& source); // 函數(shù)聲明
2
3 int main() // 主函數(shù)
4 {
5 ...........
6 }
7
8 free_throws& accumulate(free_throws & target, const free_throws & source) // 函數(shù)實(shí)現(xiàn)
9 {
10 target.attempts += source.attempts;
11 target.made += source.made;
12 set_pc(target);
13 return target;
}
五、總結(jié)
此類錯(cuò)誤不太容易發(fā)現(xiàn),讀者在敲代碼時(shí)盡量細(xì)心,犯錯(cuò)之時(shí),要學(xué)會(huì)總結(jié),利用好手上的工具。
希望可以幫助到各位!