0

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!!

4

4 回答 4

3
VehicleTwoD *vehicletwod[100];

这只会创建一个指向无效内存位置的引用数组。

您需要一个循环或其他方式来分配 100 个有效引用。例子:

for(int i=0;i < 100;i++)
{
  vehicletwod[i] = new VehicleTwoD() ;
}

你必须记住释放那些记忆。

于 2012-10-31T09:32:19.067 回答
2

添加到其他人所说的这里是一个可编译的示例(为反向排序更新/为静态类比较功能更新):

您可能需要考虑明确排序方向,而不是重载 < 运算符,例如通过指定静态比较器,如下所示。我更喜欢这个选项,因为它允许您记录您的意图。

在 Vehicle.h 中:

#include <random>
class VehicleTwoD
{

    private:
        double area;
    public:
        VehicleTwoD() 
        {
            area = ((double)rand()/(double)RAND_MAX);
        }

        double toDisplay()
        {
            return area;
        }

        bool VehicleTwoD::operator<(const VehicleTwoD& rhs) const
        {
            return area > rhs.area;
        }
        static bool VehicleTwoD::greater(const VehicleTwoD &lhs, const VehicleTwoD &rhs) 
        {
            return lhs.area > rhs.area;
        }

        static bool VehicleTwoD::lesser(const VehicleTwoD &lhs, const VehicleTwoD &rhs) 
        {
            return lhs.area < rhs.area;
        }

};

在 main.cpp 中:

#include "Vehicle.h"    
#include <vector>
#include <time.h>
#include <algorithm>
#include <iostream>


int main()
{

    srand((unsigned)time(NULL));
    std::vector<VehicleTwoD> vehicles(100);
    std::vector<VehicleTwoD>::iterator it;


    sort(vehicles.begin(), vehicles.end());
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    std::cin.get();

    // reverse sort - note that rbegin() rend() may be second-class citizens
    // depending on your compiler's implementation and that their use may therefore
    // be limited
    sort(vehicles.rbegin(), vehicles.rend());
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    std::cin.get();

    // or (document your intention)
    sort(vehicles.begin(), vehicles.end(), VehicleTwoD::greater);
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    std::cin.get();

    sort(vehicles.begin(), vehicles.end(), VehicleTwoD::lesser);
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    vehicles.clear();
    std::cin.get();
    return 0;

}
于 2012-10-31T11:01:57.267 回答
2

好吧,vehicletwod[i]->toDisplay()是 UB 因为vehicletwod[i](无论i是什么)从未初始化。

VehicleTwoD *vehicletwod[100];只需创建一个包含 100 个悬空指针的数组。

我建议你改用 a std::vector<std::unique_ptr<VehicleTwoD>>(因为它看起来你的类是多态的并且你想要多态行为)。

于 2012-10-31T09:33:10.263 回答
2

您的程序中有很多错误。

VehicleTwoD *vehicletwod[100];

创建一个包含 100 个未初始化指针的数组VehicleTwoD

sort(vehicletwod[0], vehicletwod[vehicleCounter]);

对由两个指针定义的范围进行排序,vehicletwod[0]并且vehicletwod[vehicleCounter]. 由于数组没有初始化,这些指针是垃圾,所以排序会破坏内存。

您可能需要以下内容

std::vector<VehicleTwoD> vehicletwod; // vector of instances
// initialize the vector
// ...
sort(vehicletwod.begin(), vehicletwod.end()); 
于 2012-10-31T09:34:05.230 回答