friend function in templates

This is a discussion on friend function in templates within the C++ Programming forums, part of the General Programming Boards category; Forget it... Board search gave the answer... should have read the search result completely When i try outside class defenition ...

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Exclamation friend function in templates

    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?
    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&)'
    When I try inside class defenition It works all right.
    Code:
    #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 refered some books but none of them have examples on outside class defenitions of templated friend functions
    __________________________________________
    I did a board search and I found this:
    overloading the output operator <<
    and also this in the same post:
    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;
    }
    how come operator<< is'nt a 'friend'?
    x is accessed indirectly...
    Last edited by arjunajay; 01-22-2006 at 05:07 AM. Reason: board search

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Friend function
    By Verdagon in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 12:44 PM
  4. friend function and friend classes...usage question??
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 10:53 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21