Why this fails on Linux but works on VC2010

J

Jag

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
 
F

Francesco S. Carta

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
 
A

Alf P. Steinbach /Usenet

* 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
 
A

Alf P. Steinbach /Usenet

* Alf P. Steinbach /Usenet, on 17.07.2010 02:53:
* Jag, on 17.07.2010 02:32:
^
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 );
}





Cheers & hth.,

- Alf
 
F

Francesco S. Carta

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
 
P

Paul Bibbings

Jag said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top