我試着寫一個簡單的遊戲,我無意中得到了錯誤錯誤:沒有呼叫怪物::怪物()匹配功能
"No matching function for call to Monster::Monster()"
和
"No matching function for call to Player::Player()".
兩人都是爲遊戲類構造函數。類遊戲與任何形式的怪物或玩家無關。這裏是代碼。
的main.cpp
#include <game.h>
#include <iostream>
using namespace std;
int main() {
Game game = Game();
while(!game.hasFinished()) {
game.round();
}
}
game.h
#ifndef GAME
#define GAME
#include <entity.h>
class Game {
private:
bool finished = false;
char area[15][15];
char defaultSign = '*';
int size = 15;
Monster monsters[4];
Player player;
int score = 0;
void cls();
void fill();
void display();
void playerMove();
public:
Game();
void round();
int getScore();
bool hasFinished();
};
#endif
game.cpp
#include <game.h>
#include <iostream>
#include <conio.h>
Game::Game() {
player = Player(7, 7);
monsters[0] = Monster(0, 0);
monsters[1] = Monster(size - 1, size - 1);
monsters[2] = Monster(size - 1, 0);
monsters[3] = Monster(0, size - 1);
}
bool Game::hasFinished() {
return !finished;
}
int Game::getScore() {
return score;
}
void Game::cls() {
for(int i = 0; i < 50; i++) {
std::cout << "\n";
}
}
void Game::fill() {
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
area[i][j] = defaultSign;
}
}
for(int i = 0; i < 4; i++) {
area[monsters[i].getX()][monsters[i].getY()] = monsters[i].getSign();
}
area[player.getX()][player.getY()] = player.getSign();
}
void Game::display() {
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
std::cout << area[i][j];
}
std::cout << "\n";
}
}
void Game::round() {
cls();
fill();
display();
playerMove();
for(int i = 0; i < 4; i++) {
monsters[i].moveTowards(player);
}
score++;
}
void Game::playerMove() {
bool moved = false;
while(!moved) {
char c = getch();
if(c == 'w' || c == 'W') {
player.move(player.getX(), player.getY() + 1);
moved = true;
}
if(c == 'a' || c == 'A') {
player.move(player.getX() - 1, player.getY());
moved = true;
}
if(c == 's' || c == 'S') {
player.move(player.getX(), player.getY() - 1);
moved = true;
}
if(c == 'd' || c == 'D') {
player.move(player.getX() + 1, player.getY());
moved = true;
}
}
}
entity.h
#ifndef ENTITY
#define ENTITY
class Entity {
protected:
int x, y;
char sign;
public:
Entity(int xPos, int yPos);
int getX();
int getY();
void move(int xPos, int yPos);
char getSign();
};
class Player : public Entity {
private:
char sign = 'P';
public:
Player(int xPos, int yPos);
};
class Monster : public Entity {
private:
char sign = 'M';
public:
Monster(int xPos, int yPos);
void moveTowards(Entity p);
};
#endif
entity.cpp
#include <entity.h>
Entity::Entity(int xPos, int yPos) {
x = xPos;
y = yPos;
sign = '*';
}
void Entity::move(int xPos, int yPos) {
x = xPos;
y = yPos;
}
int Entity::getX() {
return x;
}
int Entity::getY() {
return y;
}
char Entity::getSign() {
return sign;
}
Player::Player(int xPos, int yPos)
: Entity(xPos, yPos) {
}
Monster::Monster(int xPos, int yPos)
: Entity(xPos, yPos) {
}
void Monster::moveTowards(Entity p) {
int moveArea[2] = {x, y};
if(x > p.getX()) moveArea[0]--;
if(x < p.getX()) moveArea[0]++;
if(y > p.getY()) moveArea[1]--;
if(y < p.getY()) moveArea[1]++;
move(moveArea[0], moveArea[1]);
}
請幫忙!
您需要爲'Monster'提供一個默認構造函數來實例化它們的原始數組。否則,請在'Game's成員初始值設定項列表中使用大括號初始化語法。 –
太多的代碼。 – GingerPlusPlus