【问题标题】:C++ Error Code won't link [duplicate]C ++错误代码不会链接[重复]
【发布时间】:2015-06-11 21:02:10
【问题描述】:

我遇到了三个我不明白的错误。第一个说:

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

第二个说:

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

最后一句说:

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

谁能帮帮我?

在我的头文件中,我有:

     #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();
}

【问题讨论】:

  • 你需要include guards
  • @CaptainObvlious 你的意思是 indef# 吗?我如何包括警卫
  • @CaptainObvlious 我进行了更改,但仍然出现错误。我在原始帖子中包含了错误消息

标签: c++ aggregation


【解决方案1】:

这些是链接器错误,而不是编译器错误。您需要找到包含在头文件中声明的类 Player 函数的定义的源文件,然后将其添加到您的项目中进行编译和链接。您需要的文件可能名为 Player.cpp。

【讨论】:

  • 我肯定我已经添加了要编译的播放器文件
  • @Darraptor 错误消息包含短语“未定义的符号”或“未定义的引用”,这意味着您正在调用一个函数或引用一个没有定义的变量。因此,正如链接器错误所说,您缺少 Player 构造函数的定义,而“未定义对 vtable 的引用”通常意味着您缺少虚拟函数的定义(vtable = virtual table)。我看不到RespondIncorrectGuess 的定义,它是虚拟的,所以这一定是罪魁祸首。
  • 感谢 Zenith 我能够解决这个问题!还要感谢 Dan Korn 的到来以及我可能想念的任何其他人!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 2015-04-16
  • 2015-04-13
  • 2014-08-11
相关资源
最近更新 更多