2015-11-07 47 views
0

我開始在學校學習C++,並且出現此錯誤。錯誤LNK2019:無法解析的外部符號,不能與我編碼的cpp文件鏈接

1>Bettle_Dice.obj : error LNK2019: unresolved external symbol "public: int __thiscall Beetle::checkcom(void)" ([email protected]@@QAEHXZ) referenced in function _main 

我有包括其他頭文件和cpp文件,我不明白爲什麼只有這個文件有問題,請幫助

下面是我的代碼 的main.cpp

#include "stdafx.h" 
#include "beetle.h" 
#include "dice.h" 
#include "player.h" 
#include <iostream> 

using namespace std; 


int main() 
{ 
    Player p; //declare Class to a variable 
    Dice d; 
    Beetle btle; 
    int temp; 
    cout << "Number of players?" << endl; 
    cin >> temp; 

    p.Num(temp); //store the number of player into class 
    //cout << p.getNumPlayers() <<endl; 

    cout << "Start game!!" <<endl; 
    temp = btle.checkcom(); 
    while(temp != 1) 
    { 
     for(int i=0;i<p.getNumPlayers();i++) 
     { 
      temp = d.roll(); 
      cout <<"Your roll number:"<< temp; 

     } 

    } 


    return 0; 
} 

beetle.h

class Beetle 
{ 
    private: 
     int body,head,ante,leg,eye,tail; 
    public: 
     int completion(); 
     int checkcom(); 
     int getBody() { return body;}; 
     int getHead() { return head;}; 
     int getAnte() { return ante;}; 
     int getLeg() { return leg;}; 
     int getEye() { return eye;}; 
     int getTail() { return tail;}; 
}; 

Beetle.cpp

#include "stdafx.h" 
#include "beetle.h" 

int completion() 
{ 
    return 0; 
} 

int checkcom() 
{ 
    Beetle btle; 
    int flag = 0; 
    if(btle.getBody() == 1 && btle.getHead() == 1 && btle.getAnte() == 2 && btle.getEye() == 2 && btle.getLeg() ==6 && btle.getTail() == 1) 
     flag = 1; 
    return flag; 
} 

我在網上查了一些解決方案,有人說是庫問題,但這個文件不是內置函數。我想這不是圖書館的問題。我試圖將beetle.obj文件包含在其中,調試器說它已包含在內,並且有重複的定義。 在另一個文件中,我沒有「bettle」這個詞。它不應該是雙重聲明或重複類的問題。 我不知道是什麼問題。請幫忙。

+0

你可能想查看一本初學者書籍或教程[The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list )。 –

+0

經過一番閱讀,我發現我犯了一個非常簡單的錯誤。我忘記在cpp文件中的函數之前添加前綴。謝謝。 – Sing

回答

2

你需要用類名甲殼蟲::

否則編譯器只是認爲,這些功能是全局函數,而不是成員函數前綴的類函數簽名。

相關問題