错误:没有匹配函数来调用Monster :: Monster()

时间:2015-09-05 16:23:51

标签: c++

我试着写一个简单的游戏而且我意外地犯了错误

  

“没有匹配函数来调用Monster :: Monster()”

  

“没有用于调用Player :: Player()的匹配函数”。

两者都是Game类构造函数。 Class Game与Monster或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]);
}

请帮忙!

2 个答案:

答案 0 :(得分:1)

问题是

Monster monsters[4]; // ---> Means initiliaze 4 Monsters with a ctor which takes no arguments(eg default Ctor) 
Player player;  ---> //The initiliaze 1 Player with a ctor which take no arguments

因为你定义了Monster(int xPos,int yPos);默认Ctors被删除。你必须自己定义它们。实体有同样的问题。我编译了你的代码,如果你喜欢下面它会工作。

添加&#34;实体()=默认;&#34; ,初始化x和y如下,

class Entity {
    protected:
        int x = 0;  --> Critical 
        int y = 0;  --> Critical 
        char sign;
    public:
        Entity() = default; --> Critical 
        Entity(int xPos, int yPos);
        int getX();
        int getY();
        void move(int xPos, int yPos);
        char getSign();
};

为玩家和怪物做同样的事情

class Monster : public Entity {
    private:
        char sign = 'M';
    public:
        Monster(int xPos, int yPos);
        Monster() = default; --> Critical 
        void moveTowards(Entity p);
};


class Player : public Entity {
    private:
        char sign = 'P';
    public:
        Player() = default; --> Critical 
        Player(int xPos, int yPos);
};

答案 1 :(得分:0)

您应该为Monster类添加一个默认构造函数,因为在初始化Monster数组时会隐式调用它。如果您因某些原因不想这样做,可以更改Monster数组包含指向Monster对象的原始或智能指针。