我有一节简单的课
class sample
{
int i;
public:
sample(int i): i(i){}
};
int main()
{
cout << max (12, 26) << endl; // working fine
sample s1(10), s2(20);
cout << max (s1, s2); // lots of compilation errors
return 0;
}我希望max (s1,s2)应该返回最大值(s1,s2)。我知道我错过了一些东西,但无法想象这些东西。
任何帮助都将不胜感激。
德韦什
发布于 2013-10-24 09:14:33
class sample
{
public:
int i;
sample(int i): i(i){}
bool operator< (const sample& other) const
{
return i < other.i;
}
};
int main()
{
sample s1(10), s2(20);
max (s1, s2);
return 0;
}注意const在operator <之后,这是很重要的。:)
https://stackoverflow.com/questions/19561557
复制相似问题