c ++结构&方法,不正确返回

时间:2015-09-22 02:01:04

标签: c++ pointers methods struct return

我有一个子程序Map build(Map the_map)被调用来更新我的变量new_map并且地图正在更新在SUBROUTINE中但当它返回时它返回相同的地图UN-UPDATED,
注意:
我在方法内部使用cout语句进行测试,并在方法返回到交换机后进行测试...

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

结构

struct MapItem {    
    char type = 'E';    
};

struct Map {
    int size = 0;
    MapItem *items;
};

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

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;
}

1 个答案:

答案 0 :(得分:0)

尝试通过引用传递地图。

像这样:

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),并避免所有复制的复杂性。

顺便说一句:我假设您已经在代码中的其他地方为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

但我会推荐传递依据。