未解决的外部错误

发布于 2024-12-03 13:44:22 字数 1376 浏览 0 评论 0原文

我知道有很多此类问题,但我不确定我到底做错了什么。我有两个类,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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋顶上的小猫咪 2024-12-10 13:44:23

您不需要手动调用基本构造函数,如果不这样做,编译器将调用默认构造函数(除非使用虚拟继承)。您只需要实现默认构造函数 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().

这个俗人 2024-12-10 13:44:22

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

You did not implement the constructor Player::Player() that takes no arguments. And since HumanPlayer::HumanPlayer() calls the Player::Player() constructor that you never implemented, you have a problem.

嗳卜坏 2024-12-10 13:44:22

在Player.cpp中,您需要添加默认构造函数的定义。

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

但也许您不希望玩家能够匿名,在这种情况下您应该

Player();

从 Player.h 中删除该行。由于您已经声明了一个采用字符串参数的构造函数,因此编译器不会自动为 Player 类生成默认构造函数,并且没有人能够在不提供玩家名称的情况下实例化它。

In Player.cpp, you need to add the definition for the default constructor.

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文