我知道有很多这类问题,但我不确定我做错了什么。我有两个类,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/