c++ - 访问类之间动态分配的数组时出错

标签 c++ arrays class 2d

更新:我的大部分相关源代码都在这个粘贴箱中:http://pastebin.com/nhAx1jfG

有人能理解这个错误吗?我正在尝试访问我在同一命名空间的不同类中声明的二维“Tile”数组,它是公共(public)的,并且我确实在主“rouge.cpp”类中声明了一个对象。

g++ -Wall -Werror -lncurses -c rouge.cpp -o rouge.o
rouge.cpp: In function ‘void update_game(Player, Map, World, bool)’:
rouge.cpp:497: error: no match for ‘operator[]’ in ‘*(world.World::tiles + ((((unsigned int)player.Player::<anonymous>.GameObject::x) + 0xffffffffffffffffffffffffffffffffu) * 16u))[player.Player::<anonymous>.GameObject::y]’
rouge.cpp:506: error: no match for ‘operator[]’ in ‘*(world.World::tiles + ((((unsigned int)player.Player::<anonymous>.GameObject::x) + 1u) * 16u))[player.Player::<anonymous>.GameObject::y]’
rouge.cpp:515: error: no match for ‘operator[]’ in ‘*(world.World::tiles + ((unsigned int)(((unsigned int)player.Player::<anonymous>.GameObject::x) * 16u)))[(player.Player::<anonymous>.GameObject::y + -0x00000000000000001)]’
rouge.cpp:524: error: no match for ‘operator[]’ in ‘*(world.World::tiles + ((unsigned int)(((unsigned int)player.Player::<anonymous>.GameObject::x) * 16u)))[(player.Player::<anonymous>.GameObject::y + 1)]’
rouge.cpp:540: error: no match for ‘operator[]’ in ‘*(world.World::tiles + ((unsigned int)(((unsigned int)player.Player::<anonymous>.GameObject::x) * 16u)))[player.Player::<anonymous>.GameObject::y]’
make: *** [rouge.o] Error 1

世界.h:

// The "world" class, generates a world.
class World
{
    // Public classes and variables
    public:
        // Tile array
        Tile* tiles;
        int plates[WORLDSIZEX][WORLDSIZEY];
        //Tile tiles[WORLDSIZEX][WORLDSIZEY];
        //Tile (*tiles)[WORLDSIZEX] = new Tile[WORLDSIZEX][WORLDSIZEY];
        // For plates
        //int plates[WORLDSIZEX][WORLDSIZEY];
        //plates = new int[WORLDSIZEX][WORLDSIZEY];
        //int (*plates)[WORLDSIZEX] = new int[WORLDSIZEX][WORLDSIZEY];
        // Small world
        Tile smalltiles[SMALLWORLDX][SMALLWORLDY];
        // For world temp
        int worldtemp;
        // Constructor
        World();
        void generate_plates();
        bool check_plates();
        int build_mountains(int,int);
        void add_mountains();
        void generate_world();
        void shrink_world();
        void save_world();
        void erupt();
        bool get_passable(int,int);
        char get_icon(int,int);
        char get_small_icon(int,int);
        int get_color(int,int);
        int get_small_color(int,int);
};

world.cpp,我在其中分配数组:

// Constructor
World::World()
{
    // Seed for random number
    srand( time(NULL) );
    tiles = new Tile[WORLDSIZEX][WORLDSIZEY];
    // Generate a world
    generate_world();
    // Shrink world
    shrink_world();
    // Then save it.
    //save_world();
}

在 rouge.cpp 中,我访问它的方式如下:

world.tiles[i][j]; //i and j are ints in a nested for loop

在我声明之后:

World world;

它会吐出该错误

最佳答案

您使用指针指向动态分配为多维数组的数组。这个多维数组的地址存放在World::tiles之后,编译器“忘记”该数组是多维的。因此,您不能再使用双括号表示法 ( tiles[x][y] ) 来访问元素。

要解决这个问题,您至少有 4 个选择:

  1. 在 World 中提供一个方法,该方法采用 x,y 坐标并返回对所需单元格的引用(您可能希望提供 const 和非 const 版本):

    Tile& at(size_t x, size_t y) {return tiles[y*WORLDSIZEX + x];}

    const Tile& at(size_t x, size_t y) const {return tiles[y*WORLDSIZEX + x];}

  2. 汇总您自己的二维数组类 World::tiles .

  3. 使用boost::multi_array对于 World::tiles .

  4. 声明World::tilesvector< vector<Tile> > 。所有嵌套 vector 的初始大小都很棘手,但之后您可以使用 world.tiles[x][y]符号。

加上@fsmc给你的选项。


我快速浏览了 Pastebin 中的代码。如果您初始化 tiles ,它会更有效(并且看起来更干净)像这样的 vector :

tiles.resize(WORLDSIZEX);
for(int i = 0; i < WORLDSIZEX; i++)
{
    tiles[i].resize(WORLDSIZEY);
}

关于c++ - 访问类之间动态分配的数组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5671047/

相关文章:

java - 如何直接使用jni将字节数组从c复制到java byte[]属性

c++ - 获取没有访问器或修改器的嵌套类成员

c++ - 在基类中使用抽象期望它是派生类?

c++ - 析构函数默默地做了什么?

C++ for each loop with mutating vector

c++ - qlineedit 自动调整内容大小

c# - 实例化数组并一次实例化每个成员

C++将指针常量作为参数从main传递给方法

javascript - 无法将数组作为数组进行操作

javascript - 如果类(class)也是类(class),那么..单击时移动事件类(class)