Why this does not compile on g++ but works in Visual C++ 2008

J

Jag

virtex<T>* add_a_virtex(const virtex<T>& v1) {
//set<virtex<T> > _vertices ;
pair<set<virtex<T> >::iterator,bool> itt = _vertices.insert(v1) ;
/* code removed below */
}
on g++ I get this error.

sgraph.h: In member function `virtex<T>* sgraph<T>::add_a_virtex(const
virtex<T>&)':
sgraph.h:95: error: type/value mismatch at argument 1 in template
parameter list for `template<class _T1, class _T2> struct std::pair'
sgraph.h:95: error: expected a type, got `
std::set said:
::iterator'
sgraph.h:95: error: invalid type in declaration before '=' token

Can some kind soul help me to fix the issue using g++.

Thanks in advance. Please help
 
A

Alf P. Steinbach /Usenet

* Jag, on 10.07.2010 01:25:
virtex<T>* add_a_virtex(const virtex<T>& v1) {
//set<virtex<T> > _vertices ;
pair<set<virtex<T> >::iterator,bool> itt = _vertices.insert(v1) ;
/* code removed below */
}
on g++ I get this error.

sgraph.h: In member function `virtex<T>* sgraph<T>::add_a_virtex(const
virtex<T>&)':
sgraph.h:95: error: type/value mismatch at argument 1 in template
parameter list for `template<class _T1, class _T2> struct std::pair'
sgraph.h:95: error: expected a type, got `

sgraph.h:95: error: invalid type in declaration before '=' token

You're probably missing a 'typename' keyword, like

pair< typename virtex< T > >::iterator, bool > ...

If this is the problem, then T is a template parameter, and the compiler doesn't
absolutely know that virtex<T>::iterator is a type for all choices of T and
needs a little help from you about what it should assume.

This is in the FAQ somewhere, I believe.


Cheers & hth.,

- Alf
 
J

Jonathan Lee

virtex<T>* add_a_virtex(const virtex<T>& v1) {
     //set<virtex<T> >      _vertices ;
     pair<set<virtex<T> >::iterator,bool> itt = _vertices.insert(v1) ;
     /* code removed below */}

on g++ I get this error.

sgraph.h: In member function `virtex<T>* sgraph<T>::add_a_virtex(const
virtex<T>&)':
sgraph.h:95: error: type/value mismatch at argument 1 in template
parameter list for `template<class _T1, class _T2> struct std::pair'
sgraph.h:95: error:   expected a type, got `
std::set<virtex<T>,std::less<virtex<T> >,std::allocator<virtex<T> >>::iterator'

You probably just need "typename", like:
pair< typename set< virtex<T> >::iterator, bool> itt = ...;

Assuming T is the template parameter, when GCC first comes across
this it can't know that "iterator" is a type. It could be,
in theory, a static data member named "iterator". The keyword
"typename" tells the compiler you know what you're doing.

--Jonathan
 
P

Paul Bibbings

Jag said:
virtex<T>* add_a_virtex(const virtex<T>& v1) {
//set<virtex<T> > _vertices ;
pair<set<virtex<T> >::iterator,bool> itt = _vertices.insert(v1) ;
/* code removed below */
}
on g++ I get this error.

sgraph.h: In member function `virtex<T>* sgraph<T>::add_a_virtex(const
virtex<T>&)':
sgraph.h:95: error: type/value mismatch at argument 1 in template
parameter list for `template<class _T1, class _T2> struct std::pair'
sgraph.h:95: error: expected a type, got `

sgraph.h:95: error: invalid type in declaration before '=' token

Can some kind soul help me to fix the issue using g++.

The replies by Alf and Jonathan already give you the correct answer. To
answer the question in the subject line directly, VC2008 is clearly more
lax in where it requires `typename'. G++ is correct here in requiring
it, as I understand it.

On another minor point, it seems a little odd to see you adding a
`virtex' to a collection of `vertices'. The correct spelling is, of
course, `vertex'. :)

Regards

Paul Bibbings
 
P

Paul Bibbings

Sam said:
There's probably a problem somewhere in either the definition of your
virtex template class, or your _vertices object. However, because you
neglected to post your class definitions, no further help is possible.

The only thing that I can tell you is that the following complete
example which, as far as I can tell using my best guess, is
structurally equivalent with your use case, compiles just fine with
g++ 4.4.4:


#include <set>

template<typename T> class virtex {

public:
bool operator<(const virtex<T> &) const;
};

class T {

};

std::set< virtex<T> > _vertices;

void add_a_virtex(const virtex<T> &v1)
{
std::pair<std::set<virtex<T> >::iterator, bool>
itt=_vertices.insert(v1);
}

This is a complete example that compiles, but it does not contain the
crucial element of the OP's code that leads to the error message. As
you can see from the error output, add_a_virtex(const virtex<T>&) is a
member function of a parameterized type:

template<typename T>
struct sgraph {
public:
vertex<T> * add_a_vertex(const vertex<T>&);
// ...
};

You have add_a_virtex(const vertex<T>&) as a non-parameterized free
function and have had to arbitrarily define a class T in order to fit
the OP's problem to your example (which might have been a clue :). The
interpretation from both Jonathan and Alf is the correct one.

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top