C ++结构和方法,未正确返回
c++ structs & method , NOT RETURNING CORRECTLY
我有一个子程序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
但是我推荐使用引用传递
相关文章:
- 从父类方法返回子类对象
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 让bool方法返回其他整数
- 不能将方法返回的值用于另一个方法
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 从私有成员变量的成员方法返回unique_ptr
- 我的模板类方法返回错误类型?
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 方法错误"not all control paths return a value"和方法不返回值
- GRPC C++ TLS 客户端 grpc::SslCredentials() 方法不返回
- 我无法使用C++指针指向类方法返回的 std::vector
- 如何在工厂方法中返回指向基于基础操作系统的派生类的有效指针
- 将传入的网络"char*"数据转换为"uint8_t"并返回的安全方法是什么?
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- EnQueue 方法在循环队列中未正确返回C++?
- 应用于const和非const对象的引用返回方法
- 在vc++ PPL中,如何创建一个同步返回的任务返回方法
- 为什么从浮点返回方法返回双精度不会在 c++ 中导致任何错误/警告
- 这个返回方法叫什么?