I have this class
Class VehicleTwoD
{
private:
doubleArea;
}
so I want to sort by doubleArea;
at my main.cpp
I got
int main()
{
VehicleTwoD *vehicletwod[100];
sort(vehicletwod[0], vehicletwod[vehicleCounter]);
for (int i=0;i<vehicleCounter;vehicleCounter++)
{
cout << "Vehicle " << i << end;
cout << vehicletwod[i]->toDisplay() << endl;
}
}
Below is my operator overload at Vehicle.h
bool VehicleTwoD::operator<(const VehicleTwoD& rhs) const
{
return area > rhs.area;
}
When I try run the program, I get error on runtime - Segmentation core dump fault.
Assuming my Vehicle Counter is not wrong and my Vehicletwod got at least 2 object when I try run toDisplay, whats actually went wrong..
Update:
I just did this thanks to the valuable feedback I receive from the experts here.
at main.cpp
int main()
{
VehicleTwoD *vehicletwod[100];
for(int i=0;i<100;i++)
{
vehicletwod[i] = new VehicleTWoD();
}
//some computation then I go to sort..
sort(&vehicletwod[0],&vehicletwod[vehicleCounter]);
for(int i=0;i<vehicleCounter;i++)
{
cout << vehicletwod->toDisplay() << endl;
}
}
The output remain the same, nothing sorted.. I not sure why..
this is the boolean operator I did at the VehicleTwoD.h
VehicleTwoD.h
public:
bool operator<(const VehicleTwoD&) const;
VehicleTwoD.cpp
bool VehicleTwoD::operator<(const VehicleTwoD& rhs) const
{
return area > rhs.area;
}
Nothing is sorted.. I would hope at least it sort in either descending or ascending..
I was wonder if theres a way I can allocate my VehicleTwoD array into a vector call sortVector, because after sort in ascending, I wanna sort in descending also.
so a reverse function in vector will be good to solve it.
Any suggestion ? Thanks all the helpful and kind experts here!!