派生类无法正确打印

时间:2016-03-08 16:35:56

标签: c++ arrays multidimensional-array cout derived-class

我正在制作这个控制台游戏,我正在这样做的方式是从一个名为map的3维数组中向控制台打印字符。使用怪物,角色和背景的字符分配。问题是我已经能够分配给地图数组并成功打印出一个怪物类的char。但由于某种原因,派生的字符类对象没有打印其字符。 我相信这不是一个访问问题,因为没有编译错误,当程序运行时,行上的空隙(不是确切的索引,而是右行索引)是分配字符的。

以下是分配它的代码:

    void game::assignScreen() {
        for (int row = 0; row < 20; row++) {
            for (int col = 0; col < 40; col++) {
                if (col == 0 || col == 39) {
                    map[row][col] = (char) 124;
                } else if (row == 0 || row == 19) {
                    map[row][col] = (char) 95;
                } else {
                    map[row][col] = (char) 46;
                }
            }
        }
        switch (difficulty) {
            case 1:
                for (int i=0; i <2; i++){
                    map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
                }

            case 2:
                if (difficulty == 2) {
                    for (int i = 0; i <4; i++) {
                        map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
                    }
                }
                map[elfPlayer.getPos(true)][elfPlayer.getPos(false)] = elfPlayer.getSymbol();
                map[guardPlayer.getPos(true)][guardPlayer.getPos(false)] = guardPlayer.getSymbol();
                map[knightPlayer.getPos(true)][knightPlayer.getPos(false)] = knightPlayer.getSymbol();
                map[roguePlayer.getPos(true)][roguePlayer.getPos(false)] = roguePlayer.getSymbol();
                break;
            case 3:
                for (int i = 0; i <6; i++) {
                    map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
                }
            case 4:
                if (difficulty == 4) {
                    for (int i = 0; i <9; i++) {
                        map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
                    }
                }
                map[elfPlayer.getPos(true)][elfPlayer.getPos(false)] = elfPlayer.getSymbol();
                map[guardPlayer.getPos(true)][guardPlayer.getPos(false)] = guardPlayer.getSymbol();
                map[knightPlayer.getPos(true)][knightPlayer.getPos(false)] = knightPlayer.getSymbol();
                map[roguePlayer.getPos(true)][roguePlayer.getPos(false)] = roguePlayer.getSymbol();
                map[roguePlayer.getPos(true)][roguePlayer.getPos(false)] = roguePlayer.getSymbol();
                break;
        }
    }

然后打印出来

void game::printScreen(string msg) {
            clearScreen();
            cout << msg;
            for (int row = 0; row < 20; row++) {
                for (int col = 0; col < 40; col++) {
                    if (map[row][col] == 'E' || map[row][col] == 'G' || map[row][col] == 'K' ||
                        map[row][col] == 'R' || map[row][col] == 'W') {
                        if (getPlayer(map[row][col]).getStatus() == 1) {
                            changeColor(14, false);
                            cout << map[row][col];
                            changeColor(15, false);
                        } else if (getPlayer(map[row][col]).getStatus() == 2 || getPlayer(map[row][col]).getStatus() == 3) {
                            changeColor(4, false);
                            cout << map[row][col];
                            changeColor(15, false);
                        }
                    } else {
                        cout << map[row][col];
                    }
                }
                cout << endl;
            }
        }

getPlayer是一个函数,它接受当前保存/选择的char并返回它所属的对象

位置

int character::getPos(bool getX) {
    if (getX) {
        return xPos;
    } else {
        return yPos;
    }
}

返回char

 char character::getSymbol() {
        return symbol;
    }

头文件

class character
{
public:
    character();
    ~character();
    string getName();
    char getSymbol();
    int getStatus();
    int getPos(bool);
    void setPos(int, int);
    void setStatus(bool, bool);
protected:
    string className;
    char symbol;
private:
    int xPos;
    int yPos;
};

来自游戏标题文件

monster *enemy[21];
    elf elfPlayer;
    guard guardPlayer;
    knight knightPlayer;
    rogue roguePlayer;
    wizard wizardPlayer;

printScreen(elfPlayer.getName());
        cout << elfPlayer.getSymbol();

我测试了是否可以打印对象的位置,它适用于* enemy [],派生类看起来很好。我真的不知道为什么它不能正确打印。也许我犯了一个愚蠢的错误,就像一些洞察力一样。 What happens when the map is printed, the bottom right empty area is meant to be full of chars

1 个答案:

答案 0 :(得分:0)

我发现这对我来说只是一个非常愚蠢的错误。感谢Simon Kraemer发现问题。

我刚刚添加了另一个用于将内容打印到printScreen方法中的字符if语句。

顺便说一句,如果我有这样的获取位置功能,它会占用更少的空间并且使用起来更快。此外,这不是一个小组项目,所以混淆不是问题(如果我在一个组中,也许id改变它)。