question about templates

Discussion in 'C++' started by pplppp@gmail.com, Jun 16, 2005.

  1. Guest

    Hi,
    I have a simple program which I wrote and compiled on SCO Unixware
    7.1.1 (compiler is CC) with SGI STL and it compiled and worked fine.
    Now I'm using GNU GCC 4.0.0 on linux, with the GCC implementation of
    STL and I have some trouble trying compile the same code:
    the following is a segemnt of my program:


    [StringUtil.h]
    ....
    #include <string>
    using namespace std;
    ....
    template<typename T>basic_string<T> trimCopy(const basic_string<T>&
    str);
    ....

    [StringUtil.cpp]
    ....
    template<typename T>basic_string<T> trimCopy(const basic_string<T>&
    str)
    {
    basic_string<T>::size_type front = str.find_first_not_of(" \t\n");
    // line 97
    basic_string<T>::size_type end = str.find_last_not_of(" \t\n"); //
    line 98
    return str.substr(front, end-front+1); // line 99
    }
    ....

    when I try to compile the program, g++ gives me an error

    /usr/local/bin/g++ -g -frepo -O -I. -I/usr/local/include -c
    StringUtil.cpp
    StringUtil.cpp: In function 'std::basic_string<T,
    std::char_traits<_CharT>, std::allocator<_CharT> > trimCopy(const
    std::basic_string<T, std::char_traits<_CharT>, std::allocator<_CharT>
    >&)':

    StringUtil.cpp:97: error: expected `;' before 'front'
    StringUtil.cpp:98: error: expected `;' before 'end'
    StringUtil.cpp:99: error: 'front' was not declared in this scope
    StringUtil.cpp:99: error: 'end' was not declared in this scope


    I would like to know why I'm getting this error, since with the SGI STL
    implementation and SCO Unixware CC compiler I have no problem with the
    same piece of code. Any help is appreciated

    Thanks in advance

    newbie
    , Jun 16, 2005
    #1
    1. Advertising

  2. wrote:
    > I have a simple program which I wrote and compiled on SCO Unixware
    > 7.1.1 (compiler is CC) with SGI STL and it compiled and worked fine.
    > Now I'm using GNU GCC 4.0.0 on linux, with the GCC implementation of
    > STL and I have some trouble trying compile the same code:
    > the following is a segemnt of my program:
    >
    >
    > [StringUtil.h]
    > ...
    > #include <string>
    > using namespace std;
    > ...
    > template<typename T>basic_string<T> trimCopy(const basic_string<T>&
    > str);
    > ...
    >
    > [StringUtil.cpp]
    > ...
    > template<typename T>basic_string<T> trimCopy(const basic_string<T>&
    > str)
    > {


    Add keyword 'typename' here

    > basic_string<T>::size_type front = str.find_first_not_of(" \t\n");
    > // line 97


    Add keyword 'typename' here too

    > basic_string<T>::size_type end = str.find_last_not_of(" \t\n"); //
    > line 98
    > return str.substr(front, end-front+1); // line 99
    > }
    > ...


    Both 'front' and 'end' are declared to have a _dependent_type_. It is
    dependent on the meaning of 'T'. To help the compiler decide what you
    mean there, you need to say

    typename basic_string<T>::size_type front ...

    >
    > when I try to compile the program, g++ gives me an error
    >
    > /usr/local/bin/g++ -g -frepo -O -I. -I/usr/local/include -c
    > StringUtil.cpp
    > StringUtil.cpp: In function 'std::basic_string<T,
    > std::char_traits<_CharT>, std::allocator<_CharT> > trimCopy(const
    > std::basic_string<T, std::char_traits<_CharT>, std::allocator<_CharT>
    >
    >>&)':

    >
    > StringUtil.cpp:97: error: expected `;' before 'front'
    > StringUtil.cpp:98: error: expected `;' before 'end'
    > StringUtil.cpp:99: error: 'front' was not declared in this scope
    > StringUtil.cpp:99: error: 'end' was not declared in this scope
    >
    >
    > I would like to know why I'm getting this error, since with the SGI STL
    > implementation and SCO Unixware CC compiler I have no problem with the
    > same piece of code. Any help is appreciated


    V
    Victor Bazarov, Jun 16, 2005
    #2
    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. Fred
    Replies:
    1
    Views:
    598
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    380
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    468
  4. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    365
    tom_usenet
    Aug 4, 2004
  5. recover
    Replies:
    2
    Views:
    802
    recover
    Jul 25, 2006
Loading...

Share This Page