Hi,
How do you do? I need to declare a friend function to my class... okay Let me post my code
Code:
template <typename T,int N> class MySequence;
template <typename T,int N>
bool operator==(const MySequence<T,N>&, const MySequence<T,N>&);
template <typename T,int N> class MySequence
{friend bool operator== <T> (const MySequence<T,N>&,
const MySequence<T,N>&);
// my members and other methods
};
bool operator== (const MySequence<T,N>& lhs,const MySequence<T,N>& rhs)
{return lhs.value==rhs.value;
}
Well the above code works fine. Now the thing is when I compare...
Code:
MySequence<int,10> ints;
MySequence<double,10> ds;
when write
Code:
if(ints==ds)
{//some stuff
}
the compiler throws error because both are different objects.
But the comparison as shown below works fine
Code:
MySequence<int,10> ten_ints;
MySequence<int,2> two_ints;
if(ten_ints==two_ints)
{//some stuff
}
else
{//do something
}
So I commented my friend function and incorporated operator overloading in the class itself
Code:
template <typename T,int N> class MySequence
{/*friend bool operator== <T> (const MySequence<T,N>&,
const MySequence<T,N>&);*/
public:
//some methods
template <typename T2,int N2>
bool operator==(const MySequence<T,N>& lhs,
const MySequence<T2,N2>& rhs)
{
return lhs.value==rhs.value;
}
private:
//members
};
well the above works fine now i.e. I am able to compare int instance with double instance. But Is there a way to do it using friend functions?
Thanks in advance
-srikrish