C++ 矩形类和模板C++;
我试图编写一个代码,创建一个矩形类,同时创建两个类,比较两个矩形的周长和面积,并返回两个矩形中较大的一个。我不断得到错误,有一个预期的';'在我认为他们不属于的地方。有什么建议吗C++ 矩形类和模板C++;,c++,templates,C++,Templates,我试图编写一个代码,创建一个矩形类,同时创建两个类,比较两个矩形的周长和面积,并返回两个矩形中较大的一个。我不断得到错误,有一个预期的';'在我认为他们不属于的地方。有什么建议吗 #include <cstdlib> #include <iostream> #include <vector> #include <functional> using namespace std; template <typename Object, type
#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;
#包括
#包括
#包括
#包括
使用名称空间std;
模板
常量对象和findMax(常量向量和arr,比较器小于)
{
int maxIndex=0;
对于(int i=1;i
#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
语句分成较短的部分(多个shortcout
替换每个longcout
)如果<代码>;问题仍然存在,那么,实例化,看起来你需要原型<代码>矩形<代码>等等…很多其他的东西< <代码>删除>代码>语句完全是假的,一开始也可以。代码< >代码> >不是C++,你来自C吗?也许你是指<代码>比较(…
从顶部开始:您正在传递AreaComparator*
指针,指向findMax
。该函数尝试有效地执行AreaComparator*Islesson;Islesson(某物,某物)
这当然是胡说八道。你需要将你的新的对象分配给你可以操作和以后处理的变量。或者你可以简单地将它们声明为main()
中的局部标量;