简体   繁体   English

无法解决的外部错误

[英]Unresolved externals error

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. 我有两个类,Player和HumanPlayer。 HumanPlayer is supposed to inherit from the Player class. HumanPlayer应该从Player类继承。 When I'm trying to do the constructors in the HumanPlayer.cpp file and compile, I get the following errors: 当我尝试在HumanPlayer.cpp文件中执行构造函数并进行编译时,出现以下错误:

Error 2 error LNK1120: 1 unresolved externals 错误2错误LNK1120:1个未解决的外部

Error 1 error LNK2001: unresolved external symbol "public: __thiscall Player::Player(void)" (??0Player@@QAE@XZ) 错误1错误LNK2001:无法解析的外部符号“ 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. 我也在使用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){

}

You did not implement the constructor Player::Player() that takes no arguments. 您没有实现不带任何参数的构造函数Player::Player() And since HumanPlayer::HumanPlayer() calls the Player::Player() constructor that you never implemented, you have a problem. 而且由于HumanPlayer::HumanPlayer()调用了您从未实现的Player::Player()构造函数,因此您遇到了问题。

In Player.cpp, you need to add the definition for the default constructor. 在Player.cpp中,您需要为默认构造函数添加定义。

Player::Player()
: name( "anonymous-player" )
{}

But maybe you do not want a player to be able to be anonymous, in which case you should delete the line 但是也许您不希望玩家能够匿名,在这种情况下,您应该删除该行

Player();

from Player.h. 来自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. 由于您已经声明了一个带有字符串参数的构造函数,因此编译器不会自动为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()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM