L
laikon
this question is about how to define friend functions for a class
template.
the following is an example.
template <typename T>
class Array
{
private:
T* parr;
int sz;
friend ostream& operator << (ostream& os, const Array<T>& rhs);
};
template <class T>
ostream& operator << (ostream& os, const Array<T>& rhs)
{
for (int i = 0; i < rhs.sz; ++i)
os << rhs.parr << "\t";
return os;
}
these codes work well in VC++ 6.0, while linking errors are given in VC
++ 2005.
template.
the following is an example.
template <typename T>
class Array
{
private:
T* parr;
int sz;
friend ostream& operator << (ostream& os, const Array<T>& rhs);
};
template <class T>
ostream& operator << (ostream& os, const Array<T>& rhs)
{
for (int i = 0; i < rhs.sz; ++i)
os << rhs.parr << "\t";
return os;
}
these codes work well in VC++ 6.0, while linking errors are given in VC
++ 2005.