Template error

J

Jon Rea

I have the following code that works on Visual C++ 2005 and on GCC.

bool VectorContains( const std::vector<int>& vect, int value )
{
std::vector<int>::const_iterator iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}

If i try and template this, I get the following, but it only compiles on
Visual Studio, not on GCC. The error occurs when the calling code is
using an 'int' as 'T'; I have not tried others.

Anyone have any ideas? Is this a c++ syntax error on my part or should
this work? Is there a better std way to do the same thing?

template < typename T >
inline bool VectorContains( const std::vector<T>& vect, T value )
{
std::vector<T>::const_iterator iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}

error:

In file included from mmlib/manipulators/conformerbuilderbase.cpp:4:
../mmlib/tools/vector.h: In function `bool VectorContains(const
std::vector<T, std::allocator<_CharT> >&, T)':
../mmlib/tools/vector.h:72: error: expected `;' before "iter"
../mmlib/tools/vector.h:74: error: `iter' undeclared (first use this
function)
../mmlib/tools/vector.h:74: error: (Each undeclared identifier is
reported only once for each function it appears in
..)
make: *** [mmlib/manipulators/conformerbuilderbase.o] Error 1

Cheers,
Jon Rea
 
J

Jon Rea

Jon said:
I have the following code that works on Visual C++ 2005 and on GCC.

bool VectorContains( const std::vector<int>& vect, int value )
{
std::vector<int>::const_iterator iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}

If i try and template this, I get the following, but it only compiles on
Visual Studio, not on GCC. The error occurs when the calling code is
using an 'int' as 'T'; I have not tried others.

Anyone have any ideas? Is this a c++ syntax error on my part or should
this work? Is there a better std way to do the same thing?

template < typename T >
inline bool VectorContains( const std::vector<T>& vect, T value )
{
std::vector<T>::const_iterator iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}

error:

In file included from mmlib/manipulators/conformerbuilderbase.cpp:4:
./mmlib/tools/vector.h: In function `bool VectorContains(const
std::vector<T, std::allocator<_CharT> >&, T)':
./mmlib/tools/vector.h:72: error: expected `;' before "iter"
./mmlib/tools/vector.h:74: error: `iter' undeclared (first use this
function)
./mmlib/tools/vector.h:74: error: (Each undeclared identifier is
reported only once for each function it appears in
.)
make: *** [mmlib/manipulators/conformerbuilderbase.o] Error 1

Cheers,
Jon Rea

More info on this - if i change the code to this:

template < typename T >
inline
bool VectorContains( const std::vector<T>& vect, T value )
{
typedef std::vector<T>::const_iterator bob;
for(
bob iter = vect.begin();
iter != vect.end();
iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}


I get the error:

In file included from mmlib/manipulators/conformerbuilderbase.cpp:4:
../mmlib/tools/vector.h: In function `bool VectorContains(const
std::vector<T, std::allocator<_CharT> >&, T)':
../mmlib/tools/vector.h:72: error: expected init-declarator before "bob"
../mmlib/tools/vector.h:72: error: expected `,' or `;' before "bob"
../mmlib/tools/vector.h:74: error: `bob' undeclared (first use this function)
../mmlib/tools/vector.h:74: error: (Each undeclared identifier is
reported only once for each function it appears in
..)
../mmlib/tools/vector.h:74: error: expected `;' before "iter"
../mmlib/tools/vector.h:75: error: `iter' undeclared (first use this
function)
make: *** [mmlib/manipulators/conformerbuilderbase.o] Error 1

Cheers,
Jon
 
O

Ondra Holub

Jon Rea napsal:
I have the following code that works on Visual C++ 2005 and on GCC.

bool VectorContains( const std::vector<int>& vect, int value )
{
std::vector<int>::const_iterator iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}

If i try and template this, I get the following, but it only compiles on
Visual Studio, not on GCC. The error occurs when the calling code is
using an 'int' as 'T'; I have not tried others.

Anyone have any ideas? Is this a c++ syntax error on my part or should
this work? Is there a better std way to do the same thing?

template < typename T >
inline bool VectorContains( const std::vector<T>& vect, T value )
{
std::vector<T>::const_iterator iter;

Here you need
typename std::vector<T>::const_iterator iter;

Visual studio accepts it without keyword typename, but it is not
correct.
 
J

Jon Rea

Ondra said:
Jon Rea napsal:

Here you need
typename std::vector<T>::const_iterator iter;

Visual studio accepts it without keyword typename, but it is not
correct.

Thank you very much, works great!

Visual studio does annoy me with its lack of standards compliance... I
make things work in it and they don't work elsewhere ... Grrr ;-)

Cheers,
Jon
 
P

peter koch

I have the following code that works on Visual C++ 2005 and on GCC. [snip]
template < typename T >
inline bool VectorContains( const std::vector<T>& vect, T value )
{
std::vector<T>::const_iterator iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;

}
[snip]
Others have pointed out the missing typename. I just want to suggest
you to look at std::find located in <algorithm> that does almost what
you want to (returning an iterator - possibly to end). You could use
that function instead - either directly or in the body of your
VectorContains. There is no reason to reinvent the wheel, the built in
function is possibly faster and the code becomes easier to
understand.

/Peter
 

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,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top