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