简体   繁体   English

无法为二维数组对象成员赋值

[英]Can't assign value to 2d array object member

const int MAP_WIDTH = 100;
const int MAP_HEIGHT = 100;
const int RANDOM_POINTS = 10;
const int seeds = 3;

struct Point
{
    int x;
    int y;
    int tileIndex;
};

Point* points = new Point[RANDOM_POINTS];
Point* mapPoints[MAP_WIDTH][MAP_HEIGHT];

Point* nearestPoint(int x, int y)
{
    Point* ptr;
    ptr = points;
    Point* nearest_point;
    int nearest_dis = 1000;
    int distance;
    for (int i = 0; i < RANDOM_POINTS; i++, ptr++)
    {
        int dx = ptr->x;
        int dy = ptr->y;
        distance = abs(dx - x) + abs(dy - y);
        if (distance < nearest_dis)
        {
            nearest_point = ptr;
            nearest_dis = distance;
        }
    }

    return nearest_point;
}

int main(int argc,char* args[])
{
    srand(seeds);
    int min = 25;
    int max = 75;
    for (int i = 0; i < RANDOM_POINTS; i++)
    {       
        points[i].x = rand() % (max - min + 1) + min;
        points[i].y = rand() % (max - min + 1) + min;
        points[i].tileIndex = rand() & 6;
    }

    std::ofstream mapFile;
    //mapFile.open("res/mapFile.map",std::ios::app);

    for (int px = 0; px < MAP_WIDTH; px++)
    {
        for (int py = 0 ; px < MAP_HEIGHT; py++)
        {
            mapPoints[px][py]->x = px;
            mapPoints[px][py]->y = py;
            mapPoints[px][py]->tileIndex = nearestPoint(mapPoints[px][py]->x, mapPoints[px][py]->y)->tileIndex;
            std::cout << mapPoints[px][py]->x << mapPoints[px][py]->y << std::endl;
            /*if(px<MAP_WIDTH-1)
                mapFile << mapPoints[px][py]->tileIndex << ",";
            else
                mapFile << mapPoints[px][py]->tileIndex << "\n";*/      
        }
    }
    mapFile << std::endl;
    mapFile.close();
    //std::cout << nearestPoint(3, 3)->tileIndex << std::endl;
    return 0;
}

points[RANDOM_POINTS] store random generated points . points[RANDOM_POINTS]存储随机生成的点。

mapPoints[MAP_WIDTH][MAP_HEIGHT] is map size . mapPoints[MAP_WIDTH][MAP_HEIGHT]是地图大小。

The purpose of this code is output random generated tile type index to a text file ,so I can use it with tileset.png file .这段代码的目的是将随机生成的瓦片类型索引输出到一个文本文件,所以我可以将它与tileset.png 文件一起使用。

But I find out the 2d array mapPoints[MAP_WIDTH][MAP_HEIGHT] is empty .Failed to assign value to data member x ,y ,index .但我发现二维数组mapPoints[MAP_WIDTH][MAP_HEIGHT]为空。无法为数据成员 x ,y ,index 赋值。 I don't know where is the problem .我不知道问题出在哪里。

Point* mapPoints[MAP_WIDTH][MAP_HEIGHT];
...
 mapPoints[px][py]->x = px;

mapPoints is an array of pointers. mapPoints是一个指针数组。 The pointers does not point anywhere.指针不指向任何地方。 As the mapPoints is a global variable, it is initialized with nullptr .由于mapPoints是一个全局变量,所以它被初始化为nullptr nullptr is invalid pointer, dereferencing it mapPoints[px][py]->x is undefined behavior and most probably causes something similar to "segmentation fault" or other strange fault. nullptr是无效指针,取消引用它mapPoints[px][py]->x是未定义的行为,很可能会导致类似于“分段错误”或其他奇怪错误的事情。

You need to have the memory, not pointers, if you want to store something.如果你想存储一些东西,你需要有内存,而不是指针。

Point mapPoints[MAP_WIDTH][MAP_HEIGHT];
...
 mapPoints[px][py].x = px;

Also in this line:同样在这一行:

Point* points = new Point[RANDOM_POINTS];

you are allocating RANDOM_POINTS points.您正在分配RANDOM_POINTS点。 But you never delete them.但你永远不会delete它们。 This leaks memory.这会泄漏内存。 Just do:做就是了:

Point points[RANDOM_POINTS];

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

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