【问题标题】:How I can use C++ STL Sort for sorting array of object with inheritance如何使用 C++ STL Sort 对具有继承的对象数组进行排序
【发布时间】:2015-09-23 00:58:40
【问题描述】:

我尝试使用 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;
}

我试图在互联网上找到一些东西,但找不到任何可以提供帮助的东西。

【问题讨论】:

  • “一些错误”。具体是什么错误?
  • 请复制/粘贴... 确切的错误!另外:为什么不Shape* shapecir = new Circle();
  • 你能把你满是圈子的文件缩小到smallest version that reproduces the error 并发布吗?或者更好的是,硬编码?

标签: c++ arrays oop sorting stl


【解决方案1】:

您应该在编写这些函数时对其进行测试。问题出在这里:

double Shape::getArea(){
    return area;
}

bool sortByArea(const Shape * lhs, const Shape * rhs) { 
    return lhs->getArea() < rhs->getArea(); 
}

您正确地给出了 sortByArea const 指针参数,但忽略了使 getArea 成为 const 函数。编译器告诉您,在您禁止更改形状后,您正在命令代码执行可能会更改形状的操作。

【讨论】:

  • 我试图擦除 const 但仍然有很多错误。也许你可以帮我解释一下我必须做什么才能根据面积对 shapecir 进行排序。
  • @Alvin: 使getArea const;单独测试。一旦它工作,单独测试sortByArea。一旦成功,请尝试使用它对数组进行排序。
  • 这样声明:double Shape::getArea() const。并在类声明中:double getArea() const;
  • 我试过了,它解决了我之前提到的错误,但现在我得到了很多关于 sort() 的错误;
  • @Alvin: 你...测试...sortByArea...在...隔离中吗?
猜你喜欢
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
  • 2020-09-11
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
相关资源
最近更新 更多