Why this fails on Linux but works on VC2010

Discussion in 'C++' started by Jag, Jul 17, 2010.

  1. Jag

    Jag Guest

    Can some kind soul help me to fix this issue. Thanks.

    Jag
    ---------------
    template <typename T>
    class sgraph {
    public:
    friend ostream& operator<< <>(ostream& o, const sgraph<T>& s) ;
    }

    template <typename T>
    ostream& operator<<(ostream& o, const sgraph<T>& s) {


    return o ;
    }

    gcc version 3.4.6 20060404 (Red Hat 3.4.6-8)
    sgraph.h: In instantiation of `sgraph<int>':
    sgraph.cpp:26: instantiated from here
    sgraph.h:123: error: template-id `operator<< <>' for
    `std::basic_ostream<char, std::char_traits<char> >&
    operator<<(std::basic_ostream<char, std::char_traits<char> >&, const
    sgraph<int>&)' does not match any template declaration
    Jag, Jul 17, 2010
    #1
    1. Advertising

  2. Jag <>, on 16/07/2010 17:32:11, wrote:

    > Can some kind soul help me to fix this issue. Thanks.
    >
    > Jag
    > ---------------
    > template<typename T>
    > class sgraph {
    > public:
    > friend ostream& operator<< <>(ostream& o, const sgraph<T>& s) ;
    > }
    >
    > template<typename T>
    > ostream& operator<<(ostream& o, const sgraph<T>& s) {
    >
    >
    > return o ;
    > }


    I have no idea. MinGW 4.4.0 compiles the above just fine (adding the
    missing semicolon && context).

    In any case, try if you can get along with this variation:

    template<class U> friend ostream& operator<<(ostream& o, const
    sgraph<U>& s) ;

    The next time please repeat your problem in the message's body, after
    reading FAQ 5.8

    Have fun

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
    Francesco S. Carta, Jul 17, 2010
    #2
    1. Advertising

  3. * Jag, on 17.07.2010 02:32:
    > Can some kind soul help me to fix this issue. Thanks.
    >
    > Jag
    > ---------------
    > template<typename T>
    > class sgraph {
    > public:
    > friend ostream& operator<< <>(ostream& o, const sgraph<T>& s) ;

    ^
    Gobbledegook.

    > }

    ^
    Missing semicolon.


    If I were to play Microsoft Clippit, I'd say it looks as if you're attempting to
    provide an 'operator<<', and would you like some help with that?

    And if so, then here's a simple recipe:


    template< class Type >
    class SGraph
    {
    public:
    std::eek:stream& writeTo( std::eek:stream& o );
    };

    template< class Type >
    std::eek:stream& operator<<( std::eek:stream& o, SGraph const& g )
    {
    return g.writeTo( o );
    }



    > template<typename T>
    > ostream& operator<<(ostream& o, const sgraph<T>& s) {
    >
    >
    > return o ;
    > }
    >
    > gcc version 3.4.6 20060404 (Red Hat 3.4.6-8)
    > sgraph.h: In instantiation of `sgraph<int>':
    > sgraph.cpp:26: instantiated from here
    > sgraph.h:123: error: template-id `operator<< <>' for
    > `std::basic_ostream<char, std::char_traits<char> >&
    > operator<<(std::basic_ostream<char, std::char_traits<char> >&, const
    > sgraph<int>&)' does not match any template declaration



    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Jul 17, 2010
    #3
  4. * Alf P. Steinbach /Usenet, on 17.07.2010 02:53:
    > * Jag, on 17.07.2010 02:32:
    >> Can some kind soul help me to fix this issue. Thanks.
    >>
    >> Jag
    >> ---------------
    >> template<typename T>
    >> class sgraph {
    >> public:
    >> friend ostream& operator<< <>(ostream& o, const sgraph<T>& s) ;

    > ^
    > Gobbledegook.
    >
    >> }

    > ^
    > Missing semicolon.
    >
    >
    > If I were to play Microsoft Clippit, I'd say it looks as if you're
    > attempting to provide an 'operator<<', and would you like some help with
    > that?
    >
    > And if so, then here's a simple recipe:
    >
    >
    > template< class Type >
    > class SGraph
    > {
    > public:
    > std::eek:stream& writeTo( std::eek:stream& o );
    > };
    >
    > template< class Type >
    > std::eek:stream& operator<<( std::eek:stream& o, SGraph const& g )


    I meant SGraph<Type>


    > {
    > return g.writeTo( o );
    > }
    >
    >
    >
    >> template<typename T>
    >> ostream& operator<<(ostream& o, const sgraph<T>& s) {
    >>
    >>
    >> return o ;
    >> }
    >>
    >> gcc version 3.4.6 20060404 (Red Hat 3.4.6-8)
    >> sgraph.h: In instantiation of `sgraph<int>':
    >> sgraph.cpp:26: instantiated from here
    >> sgraph.h:123: error: template-id `operator<< <>' for
    >> `std::basic_ostream<char, std::char_traits<char> >&
    >> operator<<(std::basic_ostream<char, std::char_traits<char> >&, const
    >> sgraph<int>&)' does not match any template declaration

    >
    >
    > Cheers & hth.,
    >
    > - Alf
    >
    > --
    > blog at <url: http://alfps.wordpress.com>



    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Jul 17, 2010
    #4
  5. Francesco S. Carta <>, on 17/07/2010 02:48:21, wrote:

    > Jag <>, on 16/07/2010 17:32:11, wrote:
    >
    >> Can some kind soul help me to fix this issue. Thanks.
    >>
    >> Jag
    >> ---------------
    >> template<typename T>
    >> class sgraph {
    >> public:
    >> friend ostream& operator<< <>(ostream& o, const sgraph<T>& s) ;
    >> }
    >>
    >> template<typename T>
    >> ostream& operator<<(ostream& o, const sgraph<T>& s) {
    >>
    >>
    >> return o ;
    >> }

    >
    > I have no idea. MinGW 4.4.0 compiles the above just fine (adding the
    > missing semicolon && context).


    Weird, I could swear I actually /saw/ it compiling and running. I must
    have seen wrong.

    Anyway, both alternatives are fine (Alf's and mine as well).

    Go on having fun

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
    Francesco S. Carta, Jul 17, 2010
    #5
  6. Jag <> writes:

    > Can some kind soul help me to fix this issue. Thanks.
    >
    > Jag
    > ---------------
    > template <typename T>
    > class sgraph {
    > public:
    > friend ostream& operator<< <>(ostream& o, const sgraph<T>& s) ;
    > }
    >
    > template <typename T>
    > ostream& operator<<(ostream& o, const sgraph<T>& s) {
    >
    >
    > return o ;
    > }
    >
    > gcc version 3.4.6 20060404 (Red Hat 3.4.6-8)
    > sgraph.h: In instantiation of `sgraph<int>':
    > sgraph.cpp:26: instantiated from here
    > sgraph.h:123: error: template-id `operator<< <>' for
    > `std::basic_ostream<char, std::char_traits<char> >&
    > operator<<(std::basic_ostream<char, std::char_traits<char> >&, const
    > sgraph<int>&)' does not match any template declaration


    The problem here is that you are wanting to make a template function a
    friend of a templated class but, at the point at which you declare that
    friend, no prior declaration has been seen (hence the "does not match
    any template declaration").

    I have to admit to personally finding this one of the most frustrating
    forms to get correct since it requires a prior declaration of your
    templated op<< which, in turn, requires a forward-declaration of your
    sgraph class.

    The following should work for you:

    #include <iostream>

    using namespace std;

    template<typename T>
    class sgraph;

    template<typename T>
    ostream& operator<<(ostream&, const sgraph<T>&);

    template<typename T>
    class sgraph {
    public:
    friend ostream& operator<< <>(ostream&, const sgraph<T>&);
    };

    template<typename T>
    ostream& operator<<(ostream& o, const sgraph<T>&) {
    return o;
    }

    int main()
    {
    cout << sgraph<int>() << '\n';
    }

    Regards

    Paul Bibbings
    Paul Bibbings, Jul 17, 2010
    #6
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,744
    Smokey Grindel
    Dec 2, 2006
  2. ElMariachi
    Replies:
    0
    Views:
    417
    ElMariachi
    Sep 17, 2010
  3. Replies:
    0
    Views:
    288
  4. sharat

    Upgrade to VC2010

    sharat, Dec 1, 2011, in forum: C++
    Replies:
    2
    Views:
    581
    Anand Hariharan
    Dec 1, 2011
  5. Randell D.
    Replies:
    7
    Views:
    152
    Randell D.
    Oct 26, 2003
Loading...

Share This Page