Template error

Discussion in 'C++' started by Jon Rea, Jan 29, 2007.

  1. Jon Rea

    Jon Rea Guest

    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
     
    Jon Rea, Jan 29, 2007
    #1
    1. Advertising

  2. Jon Rea

    Jon Rea Guest

    Jon Rea wrote:
    > 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
     
    Jon Rea, Jan 29, 2007
    #2
    1. Advertising

  3. Jon Rea

    Ondra Holub Guest

    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.

    > for( iter = vect.begin(); iter != vect.end(); iter++ )
    > {
    > if( *iter == value )
    > {
    > return true;
    > }
    > }
    > return false;
    > }
     
    Ondra Holub, Jan 29, 2007
    #3
  4. Jon Rea

    Jon Rea Guest

    Ondra Holub wrote:
    > 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.
    >
    >> for( iter = vect.begin(); iter != vect.end(); iter++ )
    >> {
    >> if( *iter == value )
    >> {
    >> return true;
    >> }
    >> }
    >> return false;
    >> }

    >


    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
     
    Jon Rea, Jan 29, 2007
    #4
  5. Jon Rea

    peter koch Guest

    On Jan 29, 11:40 am, Jon Rea <> wrote:
    > 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
     
    peter koch, Jan 29, 2007
    #5
    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. Chris Theis
    Replies:
    2
    Views:
    486
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    548
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,128
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    790
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    538
Loading...

Share This Page