未解决的外部错误
我知道有很多此类问题,但我不确定我到底做错了什么。我有两个类,Player 和 HumanPlayer。 HumanPlayer 应该继承自 Player 类。当我尝试在 HumanPlayer.cpp 文件中执行构造函数并进行编译时,出现以下错误:
Error 2 error LNK1120: 1 unresolved externals
Error 1 error LNK2001: unresolved external symbol "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){
}
I know there's a lot of these sorts of questions but I'm not sure what I'm doing wrong exactly. I have two classes, Player and HumanPlayer. HumanPlayer is supposed to inherit from the Player class. When I'm trying to do the constructors in the HumanPlayer.cpp file and compile, I get the following errors:
Error 2 error LNK1120: 1 unresolved externals
Error 1 error LNK2001: unresolved external symbol "public: __thiscall Player::Player(void)" (??0Player@@QAE@XZ)
I read that you need to explicitly call the base class within the constructor of the derived class, so I believe I've done that since the compiler isn't throwing an error about it. If anyone could point me in the right direction, that would be great. I'm also using MS Visual Studio 2010.
Here are the files in question:
//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){
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要手动调用基本构造函数,如果不这样做,编译器将调用默认构造函数(除非使用虚拟继承)。您只需要实现默认构造函数 Player::Player()。
You don't need to call the base constructor manually, if you don't the compiler will call the default one (unless using virtual inheritance). You just need to implement that default constructor, Player::Player().
您没有实现不带参数的构造函数
Player::Player()
。由于HumanPlayer::HumanPlayer()
调用了您从未实现过的Player::Player()
构造函数,因此您遇到了问题。You did not implement the constructor
Player::Player()
that takes no arguments. And sinceHumanPlayer::HumanPlayer()
calls thePlayer::Player()
constructor that you never implemented, you have a problem.在Player.cpp中,您需要添加默认构造函数的定义。
但也许您不希望玩家能够匿名,在这种情况下您应该
从 Player.h 中删除该行。由于您已经声明了一个采用字符串参数的构造函数,因此编译器不会自动为
Player
类生成默认构造函数,并且没有人能够在不提供玩家名称的情况下实例化它。In Player.cpp, you need to add the definition for the default constructor.
But maybe you do not want a player to be able to be anonymous, in which case you should delete the line
from Player.h. Since you've declared a constructor that takes a string argument the compiler will not auto-generate a default constructor for the
Player
class, and no one will be able to instantiate it without supplying a player name.