c++ - Unresolved external 错误

标签 c++ inheritance linker

我知道有很多这类问题,但我不确定我做错了什么。我有两个类,Player 和 HumanPlayer。 HumanPlayer 应该继承自 Player 类。当我尝试在 HumanPlayer.cpp 文件中执行构造函数并进行编译时,出现以下错误:

错误 2 error LNK1120: 1 unresolved externals

错误 1 ​​error LNK2001: 未解析的外部符号“public: __thiscall Player::Player(void)” (??0Player@@QAE@XZ)

我读到您需要在派生类的构造函数中显式调用基类,所以我相信我已经这样做了,因为编译器不会抛出错误。如果有人能指出我正确的方向,那就太好了。我也在使用 MS Visual Studio 2010。

这里是有问题的文件:

//Player.h
#pragma once
#include <string>
using namespace std; 

class Player{

public: 
    Player(); 

    Player(const string &); 

    void setName(string); 
    string getName(); 
    void sowSeeds(); 

    void play(); 

private: 
    string name; 
}; 

//Player.cpp

#include <iostream>
#include "Player.h"

using namespace std;

//constructor
Player::Player(const string &enteredName){
    name = enteredName; 
}

//HumanPlayer.h

#pragma once

#include <string>
#include "Player.h"
using namespace std; 

class HumanPlayer : public Player{

public: 
    HumanPlayer(); 

    HumanPlayer(const string &); 

private: 

}; 

//HumanPlayer.cpp

#include <iostream>
#include "Player.h"
#include "HumanPlayer.h"

using namespace std;

//constructor
HumanPlayer::HumanPlayer() : Player(){

}

HumanPlayer::HumanPlayer(const string &enteredName) : Player(enteredName){

}

最佳答案

您没有实现不带参数的构造函数 Player::Player()。由于 HumanPlayer::HumanPlayer() 调用了您从未实现的 Player::Player() 构造函数,因此您遇到了问题。

关于c++ - Unresolved external 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7356499/

相关文章:

c++ - 重载运算符 : Matrix Addition

c++ - fatal error : eigen3/Eigen/Core: No such file or directory

javascript - 如何从另一个单独文件的 js 类继承 js 类?

sql - 数据库设计中的继承

c++ - ffmpeg 的 Windows 库不链接(visual studio)?

c++ - 链接器和图书馆员中的 VC++ 2012 Additional Dependencies 选项

c++ - “(void) cast”和 “__attributes__”在沉默未使用的参数警告方面有什么功能区别?

C++:如果缺少函数返回语句,for-loop 被优化为无限循环 - 编译器错误?

带参数的 JavaScript 构造函数

java - 链接库时找不到符号