-1
我有一個基類,我有4個類derived.of當然,我寫2例如類。 我創建了矢量並通過派生類的對象填充它,然後我想對我的區域函數的矢量基礎進行排序。我想使用運算符重載。在C++中對運算符重載的對象排序vector
我定義了運算符重載,但它並不完全! 請幫幫我! 謝謝....
class Shape
{
public:
Shape() {}
virtual void draw() = 0;
virtual int area() = 0;
virtual void perimeter() = 0;
bool operator >(Shape * shape_one , Shape* shape_two)
{
return shape_one->area() > shape_two->area();
}
protected :
double height;
double width;
};
class Squar : public Shape
{
public:
Squar(int _width) {
width = _width;
}
void draw(){}
int area(){
return width * width;
}
void perimeter(){}
bool operator >(Squar * squar_one , Squar* squar_two)
{
return squar_one->area() > squar_two->area();
}
};
class Triangle : public Shape
{
public:
Triangle(int _width , int _height) {
width = _width;
height = _height;
}
void draw(){}
int area(){
return (width * height)/2;
}
void perimeter(){}
bool operator >(Triangle * triangle_one , Triangle* triangle_two)
{
return triangle_one->area() > triangle_two->area();
}
};
int main()
{
Shape *rect = new Rectangular(1 , 9);
Shape *squar = new Squar(5);
QVector <Shape *> list;
list.push_back(rect);
list.push_back(squar);
retuurn 0;
}
[std :: set可能重複不檢測重複的自定義對象](https://stackoverflow.com/questions/45169444/stdset-doesnt-detect-duplicate-custom-objects)。 – StoryTeller
您不能爲指針類型重載操作符。而一個成員重載只需要一個顯式參數(左邊的操作數是'* this')。閱讀您最喜愛的C++書籍中的操作符重載。 – molbdnilo
如何讓你排序向量? QTs qsort?順便說一句,area函數應該聲明爲const,因爲它不會更改任何成員。 –