<operator
May 12, 2011 at 5:22pm UTC
Hello!
Doing a old exam atm,one of the tasks is to create a template function that does compare two flower objects by color/name.
if the name/color is equal then objects are equal.
heres what i have written so far:
overloading the operator:
1 2 3 4 5
bool Flower::operator <(const Flower&otherFlower)const
{
return this ->name<otherFlower.name&& this ->color<otherFlower.color;
}
the temlate function:
1 2 3 4 5 6 7 8 9 10
T equal<T Flower,T otherFlower>
{
if (Flower==otherFlower)
{
cout<<"YES!" <<endl;
}
else
cout<<"No" <<endl;
}
the temlate function make my compiler go crazy,dont know what i´m doing wrong..
Any help is appreciated
May 12, 2011 at 5:27pm UTC
You overload operator<, yet you attempt to use operator== in the function.
Also, the equal<T> function works with absolutely any type that defines operator==. You should use op1 and op2 as parameter names.
And if that's your declaration of a template function, then you got it all wrong. See
http://www.cplusplus.com/doc/tutorial/templates/.
Topic archived. No new replies allowed.