C++ 返回并使用指向另一个类的多维数组的指针
我试图通过room类本身中的函数访问“room”数组,即:C++ 返回并使用指向另一个类的多维数组的指针,c++,arrays,class,pointers,C++,Arrays,Class,Pointers,我试图通过room类本身中的函数访问“room”数组,即: class ship{ room * rooms[3][4]; public: room ** getrooms(){ return *rooms; } } class room{ //variables... ship * ship_ptr; public: getShipPtr(); //assume it returns the pointer to th
class ship{
room * rooms[3][4];
public:
room ** getrooms(){
return *rooms;
}
}
class room{
//variables...
ship * ship_ptr;
public:
getShipPtr(); //assume it returns the pointer to the class ship
void function_x(){
this->getShipPtr()->getrooms()->rooms[x][y]->doThat();
}
}
我在指针方面做了一些错误的事情,但我不确定,有什么可以更正代码,以便我可以访问ship类之外的room数组?
注意:假设房间已经启动
我在做一些错误的事情,但我不是很确定
您错误地假设二维数组可以衰减为指向指针的指针,就像一维数组可以衰减为指针一样
int arr1[10];
int* ptr1 = arr1; // OK. 1D array decays to a pointer.
int arr2[10][20];
int** ptr2 = arr2; // Not OK. 2D array does not decay to a pointer to pointer.
int (*ptr2)[20] = arr2; // OK. ptr2 is a pointer to "an array of 20" objects.
我的建议是:
简化代码并将界面更改为:
room* getRoom(size_t i, size_t j){
// Add checks to make sure that i and j are within bounds.
return rooms[i][j];
}
我在做一些错误的事情,但我不是很确定
您错误地假设二维数组可以衰减为指向指针的指针,就像一维数组可以衰减为指针一样
int arr1[10];
int* ptr1 = arr1; // OK. 1D array decays to a pointer.
int arr2[10][20];
int** ptr2 = arr2; // Not OK. 2D array does not decay to a pointer to pointer.
int (*ptr2)[20] = arr2; // OK. ptr2 is a pointer to "an array of 20" objects.
我的建议是:
简化代码并将界面更改为:
room* getRoom(size_t i, size_t j){
// Add checks to make sure that i and j are within bounds.
return rooms[i][j];
}
我不会坚持使用std::vector等,尽管你的老师应该从一开始就教你使用它们。让我们坚持用C++编写C。不能将二维数组作为双指针返回,因为前者不会衰减到后者。如果您坚持返回指向数组的指针,那么您需要的是
room* (*getrooms())[4] // define a function returning a pointer to array-4 of room*
{
return rooms;
}
这是因为一个2D数组的类型,例如
T arr[a][B]
衰减为指向T
数组-B的指针,我不会坚持使用std::vector
s等,尽管你的老师应该从一开始就教你使用它们。让我们坚持用C++编写C。不能将二维数组作为双指针返回,因为前者不会衰减到后者。如果您坚持返回指向数组的指针,那么您需要的是
room* (*getrooms())[4] // define a function returning a pointer to array-4 of room*
{
return rooms;
}
这是因为类型为
T arr[a][B]
的2D数组衰减为指向T
数组-B的指针,我认为问题的根本原因是让rooms
了解ship
和ship
了解房间的循环依赖性
我建议采用不同的设计:
class Ship
{
Room ship_rooms[3][4]; // Note, not dynamically allocated.
public:
void move_unit(Room& from_room, Room& destination)
{
destination.set_unit(from_room.get_unit());
}
void move_unit(unsigned int from_row, unsigned int from_column,
unsigned int dest_row, unsigned int dest_column)
{
ship_rooms[dest_row][dest_column].set_unit(ship_rooms[from_row][from_column].get_unit());
}
Room& get_room(unsigned int row, unsigned int column)
{ return ship_rooms[row][column]; }
};
在上述设计中,船
负责在房间之间移动单元。房间将获得(切割)单位或设置单位(接收)。这样就不需要一个房间来了解船的任何情况
另一种可能的设计是将某个房间移动到另一个房间:
class Room
{
Item unit;
public:
void receive_unit(const Room& other)
{
unit = other.unit;
}
void transfer_unit(Room& other)
{
other.unit = unit;
}
};
上述结构允许房间之间进行通信,而不需要了解任何有关船舶或集装箱的信息
注意:这些设计通过不需要指针来解决指针问题。而是使用引用
下层选民:请在评论中添加解释 我认为,您的问题的根本原因是让房间
了解船舶
和船舶
了解房间
的循环依赖性
我建议采用不同的设计:
class Ship
{
Room ship_rooms[3][4]; // Note, not dynamically allocated.
public:
void move_unit(Room& from_room, Room& destination)
{
destination.set_unit(from_room.get_unit());
}
void move_unit(unsigned int from_row, unsigned int from_column,
unsigned int dest_row, unsigned int dest_column)
{
ship_rooms[dest_row][dest_column].set_unit(ship_rooms[from_row][from_column].get_unit());
}
Room& get_room(unsigned int row, unsigned int column)
{ return ship_rooms[row][column]; }
};
在上述设计中,船
负责在房间之间移动单元。房间将获得(切割)单位或设置单位(接收)。这样就不需要一个房间来了解船的任何情况
另一种可能的设计是将某个房间移动到另一个房间:
class Room
{
Item unit;
public:
void receive_unit(const Room& other)
{
unit = other.unit;
}
void transfer_unit(Room& other)
{
other.unit = unit;
}
};
上述结构允许房间之间进行通信,而不需要了解任何有关船舶或集装箱的信息
注意:这些设计通过不需要指针来解决指针问题。而是使用引用
下层选民:请在评论中添加解释代码的哪个部分应该检查以确保访问<代码>房间< /代码>不是界外的吗?为了简单起见,我没有把它放在这里,并且因为这不是问题(至少现在)。CRASMSTRIT是用C++标记的,因此习惯于STD::数组和STD::vector,并且不为成员变量分配单个值。(避免新建/删除)@DieterLücking我不允许使用std::vector或std::array,我的教授坚持我采用“这种方式”为什么房间需要了解飞船?看起来要么是设计中的缺陷,要么是循环引用:ship->room->ship->room…就像有一个整数向量,每个整数都指向向量。c的哪个部分ODE应该检查以确保访问<代码>房间< /C>不是界外的吗?为了简单起见,我没有把它放在这里,因为这不是问题(至少现在)。CRASMSTRIT被标记为C++,因此习惯于STD::数组和STD::vector,并且不为成员变量分配单个值(避免新/删除)@DieterLücking我不允许使用std::vector或std::array,我的教授坚持我采用“这种方式”为什么房间需要了解飞船?看起来要么是设计上的缺陷,要么是一个循环引用:ship->room->ship->room…就像有一个整数向量,每个整数都指向向量。非常感谢,工作你是对的,我假设2D数组会像1D数组一样衰变为指针。非常感谢,工作起来很有魅力。你是对的,我假设2D数组会像1D数组一样衰变为指针。