2015-06-11 73 views
-1

我收到三个我不明白的错误。第一个说:C++错误代码将不会链接

"Player::Player()", referenced from: 
    Hangman::Hangman() in hangman.o. 

第二个说:

"vtable for Hangman", referenced from: 
    Hangman::Hangman() in hangman.o 

,最后一个云:

Hangman::~Hangman() in main.o. 

有人能帮助我吗?

在我headerfile我:

 #ifndef PLAYER_H_ 
    #define PLAYER_H_ 

    #include <iostream> 
    #include <string> 
    #include <vector> 

    using namespace std; 

    class Player{ 

    public: 
     Player(); 
     char MakeGuess(); 
     void Win(); 
     void Lose(); 
     char Agree(); 
    private: 
     string name; 
     int score; 
    }; 
#endif 

In my other headerfile I have 


#ifndef HANGMAN_H_ 
#define HANGMAN_H_ 
#include <iostream> 
#include <string> 
#include <vector> 
#include "player.h" 

using namespace std; 

class Hangman 
{ 
public: 
    Hangman(); 
    void Play(); 
protected: 
    Player player2; 
    vector<string> words; 
    int wrong; 
    const int maxwrong=4; 
    char guess; 
    void virtual RespondIncorrectGuess(); 
}; 

#endif 

在我的主要功能在不同的文件中,我有:

#include <iostream> 
#include <string> 
#include <vector> 
#include "player.h" 
#include "hangman.h" 

using namespace std; 

int main() 
{ 
    Hangman test; 

    test.Play(); 
} 
+4

你需要[include guard](http://stackoverflow.com/questions/14485215/c-class-redefinition-error-help-me-understand-headers-and-linking)。 –

+0

@CaptainObvlious你的意思是indef#thingy?我如何包括警卫 – Brogrammer93

+0

阅读链接:\ –

回答

0

这些是链接错误,不是编译器错误。您需要找到包含头文件中声明的类Player功能定义的源文件,然后添加该文件以在您的项目中进行编译和链接。你需要的文件可能叫做Player.cpp。

+0

Im正面我将播放器文件加入了编辑 – Brogrammer93

+0

@Darraptor错误消息包含短语“未定义符号”或“未定义引用”,这意味着您正在调用函数或引用到一个没有定义的变量。 因此,正如链接错误所说,你错过了“Player”构造函数的定义,而“未定义的vtable引用”通常意味着你缺少虚拟函数(vtable = virtual table)的定义。我看不到'RespondIncorrectGuess'这个虚拟的定义,因此必须是罪魁祸首。 – emlai

+0

谢谢Zenith我能解决这个问题!同时也感谢Dan Korn在那里和其他任何我可能错过的人! – Brogrammer93