我在使用一个非常简单的程序时遇到了麻烦。它抛出错误:
error C2512: 'Player' : no appropriate default constructor available
IntelliSense: no default constructor exists for class "Player"
我觉得这与在 Game.h 中将 Player 类声明为私有变量有关,但我不明白为什么。任何帮助将非常感激。
游戏.h
#pragma once
#include "Player.h"
class Game
{
public:
Game(void);
void start(void);
~Game(void);
private:
Player player;
};
游戏.cpp
#include "Game.h"
Game::Game(void)
{
Player p(100);
player = p;
}
void Game::start()
{
...
}
Game::~Game(void)
{
}
播放器.h
#pragma once
class Player
{
public:
Player(int);
~Player(void);
private:
int wallet;
};
播放器.cpp
#include "Player.h"
#include <iostream>
using namespace std;
Player::Player(int walletAmount)
{
wallet = walletAmount;
}
Player::~Player(void)
{
}