Forget it...
Board search gave the answer...
should have read the search result completely
When i try outside class defenition (as below) I get errors.
How can I solve it?
When I try inside class defenition It works all right.Code://Sample prog which generates same errors as in the original #include <iostream> using namespace std; template<int n> class A{ private: int array[n]; public: A(){ for(int i=0; i<n; ++i) array[i] = i; } friend ostream& operator<<(ostream&, const A&); }; template<int n> ostream& operator<<(ostream& out, const A<n>& a){ //What is Wrong Here??? for(int i=0; i<n; ++i) out<<a.array[i]<<",\t"; return out; } int main(){ A<10> a1; cout<<a1; return 0; } main.cpp:13: warning: friend declaration `std::ostream& operator<<(std::ostream&, const A<n>&)' declares a non-template function. main.cpp:13: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) //what does this mean??? -Wno-non-template-friend disables this warning. main.cpp: undefined reference to `operator<<(std::ostream&, A<10> const&)'
I refered some books but none of them have examples on outside class defenitions of templated friend functionsCode:#include <iostream> using namespace std; template<int n> class A{ private: int array[n]; public: A(){ for(int i=0; i<n; ++i) array[i] = i; } friend ostream& operator<<(ostream& out, const A<n>& a){ for(int i=0; i<n; ++i) out<<a.array[i]<<"\t"; return out; } }; int main(){ A<10> a1; cout<<a1; return 0; }
__________________________________________
I did a board search and I found this:
overloading the output operator <<
and also this in the same post:
how come operator<< is'nt a 'friend'?using namespace std;
template <typename T, int Num = 10>
class Bar
{
T x_;
public:
Bar(T i) : x_(i) {}
const T& x() const { return x_; }
};
template <typename T, int Num>
ostream& operator<<(ostream& o, const Bar<T,Num> &b)
{
return o<<"Num = "<<Num<<" X = "<<b.x()<<endl;
}
x is accessed indirectly...



LinkBack URL
About LinkBacks


