Define friend operator << for class template.

Discussion in 'C++' started by Joe Hesse, Dec 6, 2007.

  1. Joe Hesse

    Joe Hesse Guest

    Hi,

    I have a template class and I want to define an operator << as a friend
    function. For each instantiation of the class I want a corresponding
    instantiation of operator <<.
    The following example fails to compile with g++ version 4.1.2.
    I would appreciate it if you could help me fix it or point me to a suitable
    reference.

    Thank you,
    Joe Hesse

    ********************************************
    #include <iostream>

    // forward declaration
    template <typename T>
    class Foo;

    // forward declaration
    template <typename T>
    std::eek:stream & operator << (std::eek:stream &, const Foo<T> &);

    template <typename T>
    class Foo {
    private:
    T value;
    public:
    Foo(const T & v) : value(v) {}
    friend std::eek:stream & operator << <> (std::eek:stream &, const Foo<T> &);
    };

    // implement operator <<
    template <typename T>
    std::eek:stream & operator << (std::eek:stream &o, const Foo<T> &f) {
    return o << f.value ;
    }

    int main() {
    Foo<int> fi;
    std::cout << fi;

    return 0;
    }

    /* Here are the compiler error messages
    Test.cpp: In function int main():
    Test.cpp:26: error: no matching function for call to Foo<int>::Foo()
    Test.cpp:16: note: candidates are: Foo<T>::Foo(const T&) [with T = int]
    Test.cpp:12: note: Foo<int>::Foo(const Foo<int>&)
    */

    ********************************************
    Joe Hesse, Dec 6, 2007
    #1
    1. Advertising

  2. Joe Hesse

    Joe Hesse Guest

    Please forgive me, I have an obvious error. No response is needed from the
    newsgroup.

    "Joe Hesse" <> wrote in message
    news:...
    > Hi,
    >
    > I have a template class and I want to define an operator << as a friend
    > function. For each instantiation of the class I want a corresponding
    > instantiation of operator <<.
    > The following example fails to compile with g++ version 4.1.2.
    > I would appreciate it if you could help me fix it or point me to a
    > suitable reference.
    >
    > Thank you,
    > Joe Hesse
    >
    > ********************************************
    > #include <iostream>
    >
    > // forward declaration
    > template <typename T>
    > class Foo;
    >
    > // forward declaration
    > template <typename T>
    > std::eek:stream & operator << (std::eek:stream &, const Foo<T> &);
    >
    > template <typename T>
    > class Foo {
    > private:
    > T value;
    > public:
    > Foo(const T & v) : value(v) {}
    > friend std::eek:stream & operator << <> (std::eek:stream &, const Foo<T> &);
    > };
    >
    > // implement operator <<
    > template <typename T>
    > std::eek:stream & operator << (std::eek:stream &o, const Foo<T> &f) {
    > return o << f.value ;
    > }
    >
    > int main() {
    > Foo<int> fi;
    > std::cout << fi;
    >
    > return 0;
    > }
    >
    > /* Here are the compiler error messages
    > Test.cpp: In function int main():
    > Test.cpp:26: error: no matching function for call to Foo<int>::Foo()
    > Test.cpp:16: note: candidates are: Foo<T>::Foo(const T&) [with T = int]
    > Test.cpp:12: note: Foo<int>::Foo(const Foo<int>&)
    > */
    >
    > ********************************************
    >
    Joe Hesse, Dec 6, 2007
    #2
    1. Advertising

  3. Joe Hesse

    terminator Guest

    On Dec 6, 8:05 pm, "Joe Hesse" <> wrote:
    > Hi,
    >
    > I have a template class and I want to define an operator << as a friend
    > function. For each instantiation of the class I want a corresponding
    > instantiation of operator <<.
    > The following example fails to compile with g++ version 4.1.2.
    > I would appreciate it if you could help me fix it or point me to a suitable
    > reference.
    >
    > Thank you,
    > Joe Hesse
    >
    > ********************************************
    > #include <iostream>
    >
    > // forward declaration
    > template <typename T>
    > class Foo;
    >
    > // forward declaration
    > template <typename T>
    > std::eek:stream & operator << (std::eek:stream &, const Foo<T> &);
    >
    > template <typename T>
    > class Foo {
    > private:
    > T value;
    > public:
    > Foo(const T & v) : value(v) {}
    > friend std::eek:stream & operator << <> (std::eek:stream &, const Foo<T> &);
    >
    > };
    >
    > // implement operator <<
    > template <typename T>
    > std::eek:stream & operator << (std::eek:stream &o, const Foo<T> &f) {
    > return o << f.value ;
    >
    > }
    >
    > int main() {
    > Foo<int> fi;
    > std::cout << fi;
    >
    > return 0;
    >
    > }
    >
    > /* Here are the compiler error messages
    > Test.cpp: In function int main():
    > Test.cpp:26: error: no matching function for call to Foo<int>::Foo()
    > Test.cpp:16: note: candidates are: Foo<T>::Foo(const T&) [with T = int]
    > Test.cpp:12: note: Foo<int>::Foo(const Foo<int>&)
    > */
    >
    > ********************************************


    Instein says "take it simple, as simple as posible but not simpler".
    Why did you not use the simplest imaginable syntax?
    I would write:

    friend std::eek:stream & operator << (std::eek:stream &, const Foo &);

    but this is not what the compiler complaigns about;Please learn to
    read:

    > Test.cpp:26: error: no matching function for call to Foo<int>::Foo()
    > Test.cpp:16: note: candidates are: Foo<T>::Foo(const T&) [with T = int]
    > Test.cpp:12: note: Foo<int>::Foo(const Foo<int>&)


    Since you have defined a constructor ,C++ will no more automatically
    generate a default constructor. this is the errorneous line:

    Foo<int> fi;

    In order to resolve this add the following inside the braces for
    declaration of your template class:

    Foo(){};

    regards,
    FM.
    terminator, Dec 6, 2007
    #3
  4. Once you not provide a constructor ,There will be a default
    constructor;
    When you provide, There will be your provided constructor only;
    want.to.be.professer, Dec 7, 2007
    #4
    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:
    626
    Leor Zolman
    Apr 4, 2004
  2. Replies:
    4
    Views:
    501
    Greg Comeau
    Sep 30, 2006
  3. =?gb2312?B?wfXquw==?=
    Replies:
    10
    Views:
    684
    Victor Bazarov
    Aug 1, 2007
  4. A L
    Replies:
    1
    Views:
    502
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  5. Peter
    Replies:
    2
    Views:
    265
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page