如何使用C++STL排序对具有继承性的对象数组进行排序
How I can use C++ STL Sort for sorting array of object with inheritance
我试图使用c++STL sort()
按对象的一个属性对对象数组进行排序,但总是收到一个错误:
main.cpp: In function 'bool sortByArea(const Shape*, const Shape*)':
main.cpp:54:22: error: passing 'const Shape' as 'this' argument of 'double Shape::getArea()' discards qualifiers [-fpermissive]
return lhs->getArea() < rhs->getArea();
^
main.cpp:54:39: error: passing 'const Shape' as 'this' argument of 'double Shape::getArea()' discards qualifiers [-fpermissive]
return lhs->getArea() < rhs->getArea();
这是我的代码:
形状.cpp
#include "Shape.h"
Shape::Shape(){
width=0;
height=0;
area=0;
perimeter=0;
}
Shape::Shape(double newwidth, double newheight, double newarea, double newperimeter){
width=newwidth;
height=newheight;
area=newarea;
perimeter=newperimeter;
}
Shape::~Shape(){
}
double Shape::getWidth(){
return width;
}
double Shape::getHeight(){
return height;
}
double Shape::getArea(){
return area;
}
double Shape::getPerimeter(){
return perimeter;
}
double Shape::calArea(){
return 0;
}
double Shape::calPerimeter(){
return 0;
}
Circle.cpp
#include "Circle.h"
#include <cmath>
#define PI 3.141592654
Circle::Circle(){
width = height = 0;
area=0; perimeter=0;
}
Circle::Circle(double newradius){
width = height = newradius;
area = calArea(); perimeter = calPerimeter();
}
Circle::~Circle(){
}
double Circle::calArea(){
return (pow(width,2)*PI);
}
double Circle::calPerimeter(){
return (width * PI * 2);
}
main.cpp
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"
#include <fstream>
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int size = 200;
int cshape = 0; int rshape = 0; int sshape = 0;
void input_circle(Shape* mshape){
ifstream file;
int i;
double r;
file.open("circle.txt");
while (file >> r){
Circle crl(r);
mshape[cshape]=crl;
cshape++;
}
file.close();
}
bool sortByArea(const Shape * lhs, const Shape * rhs) {
return lhs->getArea() < rhs->getArea();
}
int main(){
Shape* shapecir;
shapecir = new (nothrow) Circle[size]();
input_circle(shapecir);
int i;
cout << "Circle" << endl;
sort(shapecir,shapecir+size,sortByArea);
for (i=0;i<cshape;i++)
cout << shapecir[i].getArea() << " " << shapecir[i].getPerimeter() << endl;
return 0;
}
我试着在网上找些东西,但找不到任何有用的东西。
您应该在编写这些函数时对它们进行了测试。问题就在这里:
double Shape::getArea(){
return area;
}
bool sortByArea(const Shape * lhs, const Shape * rhs) {
return lhs->getArea() < rhs->getArea();
}
您正确地给出了sortByArea
常量指针参数,但忽略了使getArea
成为常量函数。编译器告诉您,在您禁止更改形状之后,您正在命令代码执行可能更改形状的操作。
相关文章:
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 对象接收堆栈溢出异常 c++ 的排序向量
- 如何使用 STL 排序对具有模板专用化的自定义类对象进行排序?
- 排序谓词没有传递对索引对象的引用?
- 使用指针指向对象C++对向量进行排序
- 按类成员的顺序对包含类对象的C++向量进行排序
- 无法使用接口类型对priority_queue中的对象进行排序<T>
- 基于不同字段的对象向量的排序功能
- 基于3个对象的自定义排序
- 尝试使用比较运算符对对象向量进行排序
- C++排序的对象集
- 从文件中读取并将其内容放入对象数组中,然后对它们进行排序
- q排序类对象的列表
- std::atexit 从全局对象的构造函数调用时的排序
- 尝试对对象数组进行排序时程序终止
- std::排序为排序自定义对象时出现的向量引发错误
- 使用sort()算法C 排序对象列表
- 使用std::sort与自定义排序对象和数据类型模板-寻找正确的语法
- 快速方法部分排序对象
- 如何在动态内存中排序对象列表,在c++中使用泛型向量