矩形类和模板

Rectangle Class and Templates C++

本文关键字:      更新时间:2023-10-16

我试图写一个代码,创建一个矩形类,也创建两个类,比较周长和两个矩形的面积,并返回较大的两个。我不断得到错误,在我认为不属于它们的地方有一个预期的';'。有什么建议吗?

#include <cstdlib>
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
template <typename Object, typename Comparator>
const Object & findMax( const vector<Object> & arr, Comparator isLessThan )
{
    int maxIndex = 0;
    for( int i = 1; i < arr.size(); ++i )
      if( isLessThan( arr[ maxIndex], arr[ i ] ) )
          maxIndex = i;
  return arr[ maxIndex ];
}
class AreaComparator
{
  public: 
    int compare( Rectangle lhs, Rectangle rhs ) const
  { return double.compare( lhs.getArea(), rhs.getArea() ); }
};
class PeriComparator
{
  public:
    int compare( Rectangle lhs, Rectangle rhs ) const
  { return double.compare( lhs.getPerimeter(), rhs.getPerimeter() ); }
};
class Rectangle
{
  double length, width;
  public:
    Rectangle( double l, double w) 
  {
        l = length;
        w = width;
  }
    double getArea()
  {
        return length * width;
  }
    double getPerimeter()
  {
        return ( 2 * length) + ( 2 * width );
  }
};
int main(int argc, char *argv[])
{
    cout <<(findMax(new Rectangle[] { new Rectangle(1, 5), new Rectangle(2,    3) }, new AreaComparator())) << endl;
cout <<(findMax(new Rectangle[] { new Rectangle(1, 5), new Rectangle(2, 3) }, new PeriComparator())) << endl;
system("PAUSE");
delete Rectangle[];
delete AreaComparator[];
delete PeriComparator[];
return EXIT_SUCCESS;

}

有什么建议吗?

基于@Logicrat的评论,我建议将所有新对象分配给局部变量名,然后将您的cout语句拆分为更短的部分(多个短cout取代每个长cout)。如果;问题仍然存在,您将更容易看到原因。

相关文章:
  • 没有找到相关文章