Overloading operator<< as a friend to a template class

Discussion in 'C++' started by CrimzonRJ@gmail.com, Sep 30, 2006.

  1. Guest

    Theoretically, how are you supposed to overload << as friend to a
    template class?

    I keep getting an 'unresolved extern symbol' error.

    Is this even a smart thing to do?
     
    , Sep 30, 2006
    #1
    1. Advertising

  2. lw1a2 Guest

    wrote:
    > Theoretically, how are you supposed to overload << as friend to a
    > template class?
    >
    > I keep getting an 'unresolved extern symbol' error.
    >
    > Is this even a smart thing to do?


    example:

    #include <iostream>
    using namespace std;

    template <class T>
    class A;

    template <class T>
    ostream& operator<<(ostream& os, const A<T>& a);

    template <class T>
    class A
    {
    friend ostream& operator<< <T>(ostream& os, const A&);
    public:
    A(T i, T j):i(i), j(j){}
    private:
    T i;
    T j;
    };

    template <class T>
    ostream& operator<<(ostream& os, const A<T>& a)
    {
    os<<"( "<<a.i<<", "<<a.i<<" )";
    return os;
    }

    int main()
    {
    A<int> a(1, 1);
    cout<<a<<endl;
    system("pause");
    return 0;
    }
     
    lw1a2, Sep 30, 2006
    #2
    1. Advertising

  3. Guest

    I am writing the program with Visual Studio 2005 and there is a catch22
    involved:
    The problem is that the export keyword is not supported therefore I am
    forced to put the template Implementations in the header file(is this
    correct?).
     
    , Sep 30, 2006
    #3
  4. lw1a2 Guest

    plaster your code
     
    lw1a2, Sep 30, 2006
    #4
  5. Greg Comeau Guest

    In article <>,
    <> wrote:
    >I am writing the program with Visual Studio 2005 and there is a catch22
    >involved:
    >The problem is that the export keyword is not supported therefore I am
    >forced to put the template Implementations in the header file(is this
    >correct?).


    Right, but a quick skim shows lw1a2 gave a sample that you would
    put in the header file. How is your code different?

    You may also wanna have a look at:
    http://www.comeaucomputing.com/techtalk/templates/#export
    --
    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Sep 30, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yu Lianqing
    Replies:
    1
    Views:
    651
    Leor Zolman
    Apr 4, 2004
  2. Robert Wierschke
    Replies:
    3
    Views:
    11,099
    Louise56
    Aug 3, 2004
  3. Gerhard Pfeiffer
    Replies:
    3
    Views:
    436
    Victor Bazarov
    Sep 14, 2006
  4. Replies:
    6
    Views:
    686
    James Kanze
    May 15, 2009
  5. A L
    Replies:
    1
    Views:
    522
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page