C ++结构和方法,未正确返回

c++ structs & method , NOT RETURNING CORRECTLY

本文关键字:返回 方法 结构      更新时间:2023-10-16

我有一个子程序Map build(Map the_map)被调用来更新我的变量new_map,并且地图正在子程序内更新,但是当它返回时,它返回未更新的相同地图,
注意:
我测试了方法内的cout语句,也在方法返回到开关后…

下面是我的代码片段:
1 .结构细节
2和子程序被调用的时刻
3和子程序代码

结构体

struct MapItem {    
    char type = 'E';    
};
struct Map {
    int size = 0;
    MapItem *items;
};

在switch中调用的子程序(如果用户选择构建选项)在map上构建

case BUILD:
                new_map = build(new_map);
                break;

子例程

Map build (Map the_map) {
    char building_code;
    int coordinate_x;
    int coordinate_y;
    int build_location;
    cout << "Enter x and y coordinate: ";
    cin  >> coordinate_x;
    cin  >> coordinate_y;
    build_location = (coordinate_x+(coordinate_y*the_map.size));
    cout << "Enter a building code: ";
    cin >> building_code;
    the_map.items[build_location].type = building_code;
    return the_map;
}

尝试通过引用传递映射。

:

void build (Map& the_map) {
    char building_code;
    int coordinate_x;
    int coordinate_y;
    int build_location;
    cout << "Enter x and y coordinate: ";
    cin  >> coordinate_x;
    cin  >> coordinate_y;
    build_location = (coordinate_x+(coordinate_y*the_map.size));
    cout << "Enter a building code: ";
    cin >> building_code;
    the_map.items[build_location].type = building_code;
}

case BUILD:
                build(new_map);
                break;

当你通过引用传递时,你可以直接修改变量(即map),从而避免了所有复制的复杂性。

BTW:我假设你已经在代码的其他地方为MapItems保留了内存,但你可能需要在你的构建函数内部进行一些范围检查,以便你不会在分配的内存之外编写。

如果你正确地初始化了东西,你的初始代码应该没问题。我试着这样做:

    Map build(Map the_map) {
    char building_code;
    int coordinate_x;
    int coordinate_y;
    int build_location;
    cout << "Enter x and y coordinate: ";
    cin >> coordinate_x;
    cin >> coordinate_y;
    build_location = (coordinate_x + (coordinate_y*the_map.size));
    // Range check
    if ((build_location >= (the_map.size*the_map.size)) || (build_location < 0))
    {
        cout << "Invalid" << endl;
        return the_map;
    }
    cout << "Enter a building code: ";
    cin >> building_code;
    the_map.items[build_location].type = building_code;
    return the_map;
}

int main()
{
    // Just a const so that map size can be change here
    const int mapSize = 3;
    // Create the map
    Map new_map;
    // Initialize map size
    new_map.size = mapSize;
    // Allocate memory for the MapItems
    new_map.items = new MapItem[mapSize * mapSize];
    // Do some simple test...
    for (int i = 0; i < mapSize * mapSize; i++) cout << new_map.items[i].type << " ";
    cout << endl;
    for (int j = 0; j < 3; j++)
    {
        new_map = build(new_map);
        for (int i = 0; i < mapSize * mapSize; i++) cout << new_map.items[i].type << " ";
        cout << endl;
    }
    // Deallocate memory
    delete[] new_map.items;
    return 0;
}

E E E E E E E E E
Enter x and y coordinate: 2 0
Enter a building code: Y
E E Y E E E E E E
Enter x and y coordinate: 0 2
Enter a building code: Z
E E Y E E E Z E E
Enter x and y coordinate: 2 2
Enter a building code: P
E E Y E E E Z E P

但是我推荐使用引用传递