所以我一直在努力解决这个问题。我修复了所有的内存probs(逻辑好几天),现在我被困在看似最简单的东西上。我必须在它们已满时扩展两个数组,这会扩展它们,但是不会复制数据,有关从何处开始的任何建议?
List::List(const List& x, int b)
{
if(b == 1) // if car needs to grow
{
size1 = x.size1 * 2;
car = new Car[size1];
for(int i = 0; i < count1; i++)
car[i] = x.car[i]; // what copies .. I think...
}
else if(b == 2) // if motorcycle array needs to grow
{
size2 = x.size2 * 2;
motor = new Motorcycle[size2];
for(int i = 0; i < count2; i++)
{
motor[i] = x.motor[i]; // what copies .. I think...
}
}
}
我在这两个类中都有一个asignment运算符重载
Car& Car::operator=( Car x)
{
this->setModel(x.rModel());
this->setMake(x.rMake());
this->setPrice(x.Vehicle::getPrice());
this->setMileage(x.rMiles());
this->setLot(x.rLot());
this->setGas(x.rGas());
this->seats = x.rSeats();
this->luxury = x.rLux();
}
同样在摩托车
Motorcycle& Motorcycle::operator=( Motorcycle x)
{
this->setModel(x.rModel());
this->setMake(x.rMake());
this->setPrice(x.Vehicle::getPrice());
this->setMileage(x.rMiles());
this->setLot(x.rLot());
this->setGas(x.rGas());
this->style = x.rStyle();
this->passenger = x.rPassenger();
}
如果你帮助我,我会把你拉出来的一绺头发送给你。
完整代码:https://drive.google.com/folderview?id=0B7LaFMkHcgCAd3habUpjWUR0YU0&usp=sharing
答案 0 :(得分:1)
在您的代码中,您可能错过了重新分配声明
car = new Car[size1];
for(int i = 0; i < count1; i++)
car[i] = x.car[i];
之后你应该添加另一行让x再次指向汽车
x = car;
类似于电机!
希望这有帮助,但如果你想要动态数组,那么尝试使用向量(事实上它们很快:p)