【发布时间】:2012-05-19 19:29:46
【问题描述】:
我的家庭作业需要帮助。我需要为停车场编写代码。为了编写它,我需要将我的类“Parkbox”实例的输入与#define EMPTY "--------". 一起创建在堆上并通过另一个类“Parkinggarage”创建
所以这是我的代码:
Parkbox 定义:
class Parkbox{
char *license_plate; // car's license plate
public:
Parkbox(); //Default CTOR
Parkbox(char * ); // CTOR
~Parkbox(); // DTOR
void show();
};
and ParkingGarage:
class ParkingGarage{
Parkbox ***p2parkboxes;
和我的 CTOR 或 ParkingGarage 以便在堆上创建 Parkbox 实例:
ParkingGarage::ParkingGarage(const int rw,const int clm, const int plns){
p2parkboxes = new Parkbox **[plns];//points to the floors and make the arraq of p2p same size as number as floors
for(int p=0;p<plns;p++){
p2parkboxes[p]= new Parkbox *[plns];//for each Plane creats an array of pointer that is same with the num of rows
for(int r=0;r<rw;r++)
p2parkboxes[p][r]= new Parkbox [clm];
}
}
void ParkingGarage::find_next_free_parking_position()
{
for(int f=0;f<dimensions_of_parkhouse[0];f++){
for(int r=0;r<dimensions_of_parkhouse[1];r++){
for (int c=0;c<dimensions_of_parkhouse[2];c++){
//p2parkboxes[f][r][c] is the instance of the class Pakbox
if(p2parkboxes[f][r][c]==EMPTY)
{
next_free_parking_position[0]=p;
next_free_parking_position[1]=r;
next_free_parking_position[2]=c;
}
}
}
}
}
在“p2parkboxes[f][r][c]==EMPTY”这一点上,它给了我一个错误,即“没有运算符”== 匹配这些操作数”,。
那么如何直接将类实例与 EMPTY 等其他变量进行比较?
我不知道我是否清楚你。 但是请帮助我,因为如果我不解决这个问题,我将无法继续完成我的代码。
【问题讨论】:
-
能把
p2parkboxes[f][r][c]的类型的定义贴出来吗? -
是的,这是声明:class ParkingGarage{ Parkbox ***p2parkboxes;我在 CTOR og parkgarage 中创建了对象: ParkingGarage::ParkingGarage(const int rw,const int clm, const int plns){ p2parkboxes = new Parkbox **[plns];// 指向地板并制作 arraq p2p 与楼层数相同的大小 for(int p=0;p
-
这是 Parkbox 类头:class Parkbox{ char *license_plate; // 汽车的车牌 public: Parkbox(); //默认 CTOR Parkbox(char * ); // CTOR ~Parkbox(); // DTOR 无效显示(); };
-
您可以使用您在 cmets 中发布的代码来编辑您的原始问题。
-
如果我们不知道什么是允许的,就很难提供帮助。比较类实例的唯一方法是通过运算符重载,因此如果不允许这样做,您可能应该创建一个 getter 函数并比较其结果:
strcmp(p2parkboxes[f][r][c].getPlate(), EMPTY)。希望您能尽快了解容器和 stings,因为现在,您并没有真正学习 C++。