#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; class Shape { public: virtual double getArea() = 0; virtual string getName() = 0; void setWidth(double w) { width = w; } void setHeight(double h) { height = h; } protected: double width; double height; }; class Rectangle: public Shape { public: double getArea() { return (width * height); } string getName() { return "矩形"; } }; class Triangle: public Shape { public: double getArea() { return (width * height)/2; } string getName() { return "三角形"; } }; template<typename T> double getTotalArea(vector<T>& shapes) { double total = 0; for (typename vector<T>::iterator it = shapes.begin(); it != shapes.end();++it) { total += *it->getArea(); } return total; } class CompareShapes { public: bool operator()(Shape* a, Shape* b) { return a->getArea() < b->getArea(); } }; int main() { vector<Shape*> shapes; Rectangle rect1; rect1.setWidth(5); rect1.setHeight(7); Rectangle rect2; rect2.setWidth(3); rect2.setHeight(4); Triangle tri1; tri1.setWidth(5); tri1.setHeight(7); Triangle tri2; tri2.setWidth(3); tri2.setHeight(4); shapes.push_back(&rect1); shapes.push_back(&rect2); shapes.push_back(&tri1); shapes.push_back(&tri2); cout << "图形集合的总面积为:" << getTotalArea(shapes) << endl; sort(shapes.begin(), shapes.end(), CompareShapes()); cout << "图形集合中面积最大的图形是:" << shapes.back()->getName() << endl; return 0; }此段代码有错误不能运行,,请帮忙改正
时间: 2024-01-24 22:02:28 浏览: 23
在 `getTotalArea` 函数的 for 循环中,应该改为 `total += (*it)->getArea();`,因为 `it` 是一个指向指针的迭代器,需要先解引用得到指针,再调用其成员函数。另外,类的定义应该放在 `main` 函数之前。以下是修改后的代码:
```
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class Shape {
public:
virtual double getArea() = 0;
virtual string getName() = 0;
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
protected:
double width;
double height;
};
class Rectangle : public Shape {
public:
double getArea() {
return (width * height);
}
string getName() {
return "矩形";
}
};
class Triangle : public Shape {
public:
double getArea() {
return (width * height) / 2;
}
string getName() {
return "三角形";
}
};
template<typename T>
double getTotalArea(vector<T>& shapes) {
double total = 0;
for (typename vector<T>::iterator it = shapes.begin(); it != shapes.end(); ++it) {
total += (*it)->getArea();
}
return total;
}
class CompareShapes {
public:
bool operator()(Shape* a, Shape* b) {
return a->getArea() < b->getArea();
}
};
int main() {
vector<Shape*> shapes;
Rectangle rect1;
rect1.setWidth(5);
rect1.setHeight(7);
Rectangle rect2;
rect2.setWidth(3);
rect2.setHeight(4);
Triangle tri1;
tri1.setWidth(5);
tri1.setHeight(7);
Triangle tri2;
tri2.setWidth(3);
tri2.setHeight(4);
shapes.push_back(&rect1);
shapes.push_back(&rect2);
shapes.push_back(&tri1);
shapes.push_back(&tri2);
cout << "图形集合的总面积为:" << getTotalArea(shapes) << endl;
sort(shapes.begin(), shapes.end(), CompareShapes());
cout << "图形集合中面积最大的图形是:" << shapes.back()->getName() << endl;
return 0;
}
```