C++ - 没有合适的默认构造函数可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19233174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:38:08  来源:igfitidea点击:

C++ - No appropriate default constructor available

c++classinheritance

提问by RobotEyes

I'm having trouble with a very simple program. It throws the errors:

我在一个非常简单的程序上遇到了麻烦。它抛出错误:

error C2512: 'Player' : no appropriate default constructor available

error C2512: 'Player' : no appropriate default constructor available

IntelliSense: no default constructor exists for class "Player"

IntelliSense: no default constructor exists for class "Player"

I have a feeling it's got something to do with declaring the Player class as a private variable in the Game.h but I can't see why. Any help would be much appreciated.

我有一种感觉,这与在 Game.h 中将 Player 类声明为私有变量有关,但我不明白为什么。任何帮助将非常感激。

Game.h

游戏.h

#pragma once
#include "Player.h"

class Game
{
public:
    Game(void);
    void start(void);
    ~Game(void);
private:
    Player player;
};

Game.cpp

游戏.cpp

#include "Game.h"

Game::Game(void)
{
    Player p(100);

    player = p;
}

void Game::start()
{
    ...
}

Game::~Game(void)
{
}

Player.h

播放器.h

#pragma once
class Player
{
public:
    Player(int);
    ~Player(void);

private:
    int wallet;
};

Player.cpp

播放器.cpp

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

using namespace std;

Player::Player(int walletAmount)
{
    wallet = walletAmount;
}

Player::~Player(void)
{
}

回答by Joachim Isaksson

In contrast to C#, this declaration;

与 C# 相比,此声明;

Player player;

...is an instantiation of the type Player, which means that by the time you're assigning it inside the constructor, it has already been constructed without a parameter.

...是 type 的实例化Player,这意味着当您在构造函数中分配它时,它已经被构造为没有参数。

What you need to do is to tell the class how to initialize playerin what is called an initializer listthat you append to the constructor header;

您需要做的是告诉类如何player在附加到构造函数头的所谓的初始化列表中进行初始化

Game::Game(void) : player(100)
{
...

...which tells the compiler to use that constructor to initialise playerin the first place instead of first using the default no-parameter constructor and thenassigning to it.

...它告诉编译器首先使用该构造函数进行初始化player,而不是首先使用默认的无参数构造函数然后分配给它。

回答by hob

In general - not in this case - the error

一般来说 - 不是在这种情况下 - 错误

no appropriate default constructor available

may also occur, if you forgot to include the header file for the related object.

如果您忘记包含相关对象的头文件,也可能发生这种情况。

This happened to me, so I wanted to add this solution here.

这发生在我身上,所以我想在这里添加这个解决方案。

回答by LihO

When you construct instance of Game, it tries to construct its member playerusing the default constructor. Initialize playerwithin the initializer list, not inside the body of constructor:

当您构造 的实例时Game,它会尝试player使用默认构造函数来构造其成员。初始化player初始化列表中,而不是构造函数体内部:

Game::Game() : player(100)
{
    ...
}

回答by Ivan Grynko

Only Player::Player(int)exists, so you need to initialize player in Game::Game()

Player::Player(int)存在,所以你需要在Game::Game()

Game::Game(void)
    : player( 100 )
{
}