简体   繁体   English

创建类C ++的2D数组

[英]Creating a 2D Array of a Class C++

I'm using a C++ engine called libtcod to print out ASCII characters in a rogue-like game. 我正在使用名为libtcod的C ++引擎在类似流氓的游戏中打印出ASCII字符。

In the game I want to represent the 'map' as a 2D array of type Map, which contains 'layer1' for tiles, eg Grass, Sand, Water, and a second layer to represent objects on top of layer2, eg Trees and Boulders. 在游戏中,我想将“地图”表示为Map类型的2D数组,其中包含用于图块(例如草,沙,水)的“ layer1”,以及用于表示layer2顶部的对象(例如树木和巨石)的第二层。

I have created the Map class, which contains two attributes; 我创建了Map类,其中包含两个属性; one of type Tile, and the other of type Object. 一个是Tile类型,另一个是Object类型。

However I seem to be having difficulties in the creation of the 2D array itself. 但是,我似乎在创建2D数组本身时遇到困难。

I am getting the error: "an array may not have elements of this type" 我收到错误消息:“数组可能没有这种类型的元素”

#include "libtcod.hpp"
#include <ctime>
#include <stdio.h>
#include <ciso646>
#include <cstring>

static const TCODColor colour_darkWall(128, 128, 128);
static const TCODColor colour_grass(0, 255, 0);
static const TCODColor colour_black(0, 0, 0);
static const TCODColor colour_white(255, 255, 255);
static const TCODColor colour_blue(0, 0, 255);
static const TCODColor colour_brown(139, 69, 19);
static const TCODColor colour_red(255, 0, 0);

int i, j;

class object {
public:
    char name[20];
    TCODColor colour;
    char symbol;
    bool passable;
    object(int layer_id) {
        if (layer_id == 0) { //EMPTY
            strcpy(name, "NA");
            symbol = ' ';
            passable = true;
            colour = colour_white;
        }
        else if (layer_id == 1) { //TREE
            strcpy(name, "Tree");
            symbol = '^';
            passable = false;
            colour = colour_grass;
        }
        else if (layer_id == 2) { //ROCK
            strcpy(name, "Rock");
            symbol = 'R';
            passable = false;
            colour = colour_black;
        }
    }
};

class tile {
public:
    char name[20];
    TCODColor colour;
    bool passable;
    tile(int layer_id) {
        if (layer_id == 0) { //WATER
            strcpy(name, "Water");
            colour = colour_blue;
            passable = false;
        }
        else if (layer_id == 1) { //GRASS
            strcpy(name, "Grass");
            colour = colour_grass;
            passable = true;
        }
        else if (layer_id == 2) { //SAND
            strcpy(name, "Sand");
            colour = colour_brown;
            passable = true;
        }
    }

};

class Map {
public:
    tile layer1;
    object layer2;
    Map(int layer1_id, int layer2_id) {
        layer1 = tile(layer1_id);
        layer2 = object(layer2_id);
    }
};


int main() {
    const int window_x = 150;
    const int window_y = 50;

    Map[][] map = new Map[5][5];


    TCODConsole::initRoot(window_x, window_y, "CivSim v0.1", false);
    while (!TCODConsole::isWindowClosed()) {
        TCODConsole::root->clear();
        TCODConsole::root->putChar(0, 0, map.layer2.symbol);
        TCODConsole::flush();
    }

    return 0;
}

Not sure how to create the 2D array in question. 不确定如何创建有问题的2D数组。

Thanks in advance for any help! 在此先感谢您的帮助!

Map[][] map = new Map[5][5]; is not legal. 是不合法的。 Only the first dimension can be omitted. 仅第一维可以省略。 You need Map[][5] map = new Map[5][5]; 您需要Map[][5] map = new Map[5][5]; or better still Map (*map)[5] = new Map[5][5]; 或者更好的是Map (*map)[5] = new Map[5][5]; .

The reason is that these notations using [] in a type are just another (and confusing) way of describing a pointer type. 原因是这些在类型中使用[]符号只是描述指针类型的另一种方法(而且令人困惑)。 So int[] x is just a different way of writing int* x and Map[][5] m is just another way of writing Map (*m)[5] (which is to say a pointer to an array of five Maps). 因此, int[] x只是写int* x另一种方式,而Map[][5] m只是写Map (*m)[5]另一种方式(也就是说,指向五个Maps的数组的指针) )。

I think you should always use the pointer version of these notations, it's more correctly describes what you are really doing. 我认为您应该始终使用这些符号的指针版本,它可以更正确地描述您的实际工作。 In the long term writing code that looks like you have an array, when all you actually have is a pointer is confusing. 从长远来看,看起来像您有一个数组的代码,当您实际拥有的只是一个指针时,很容易混淆。

So why isn't Map[][] another way of writing Map** ? 那么,为什么Map[][]不是写Map**另一种方式呢? It's because the similarity between arrays and pointer in C and C++ only works for one level. 这是因为C和C ++中数组与指针之间的相似性仅适用于一个级别。 A two dimensional array is a fundamentally different structure from a pointer to an array of pointers. 二维数组是从指针到指针数组的根本不同的结构。 So int[5][5] isn't compatible with int** so there is no reason to make int[][] mean the same as int** . 因此int[5][5]int**不兼容,因此没有理由使int[][]int**相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM