STL problem.

Discussion in 'C++' started by Todd A. Anderson, Nov 2, 2005.

  1. #include <map>

    using namespace std;

    template <typename S, typename T>
    void bar(void) {
    map<S,T>::iterator theiter;
    }

    int main(void) {
    return 0;
    }

    At the declaration of theiter, gcc (both 3.4 and 4.0) gives me:

    error: expected ';' before "theiter"

    Am I totally braindead or why does this not work?
     
    Todd A. Anderson, Nov 2, 2005
    #1
    1. Advertising

  2. Todd A. Anderson

    Guest

    hi
    i use VC6 it's OK!!

    cheer!
     
    , Nov 2, 2005
    #2
    1. Advertising

  3. Todd A. Anderson

    Nan Li Guest

    Todd A. Anderson wrote:
    > #include <map>
    >
    > using namespace std;
    >
    > template <typename S, typename T>
    > void bar(void) {
    > map<S,T>::iterator theiter;
    > }
    >
    > int main(void) {
    > return 0;
    > }
    >
    > At the declaration of theiter, gcc (both 3.4 and 4.0) gives me:
    >
    > error: expected ';' before "theiter"
    >
    > Am I totally braindead or why does this not work?



    You need to use 'typename' to tell compiler iterator is a dependant
    type, not a member varible of map. The following should work.


    #include <map>

    using namespace std;

    template <typename S, typename T>
    void bar(void) {
    typename map<S,T>::iterator theiter;

    }

    int main(void) {
    return 0;

    }
     
    Nan Li, Nov 2, 2005
    #3
  4. "Nan Li" <> wrote in message
    news:...
    >
    > Todd A. Anderson wrote:
    >> #include <map>
    >>
    >> using namespace std;
    >>
    >> template <typename S, typename T>
    >> void bar(void) {
    >> map<S,T>::iterator theiter;
    >> }
    >>
    >> int main(void) {
    >> return 0;
    >> }
    >>
    >> At the declaration of theiter, gcc (both 3.4 and 4.0) gives me:
    >>
    >> error: expected ';' before "theiter"
    >>
    >> Am I totally braindead or why does this not work?

    >
    >
    > You need to use 'typename' to tell compiler iterator is a dependant
    > type, not a member varible of map. The following should work.
    >
    >
    > #include <map>
    >
    > using namespace std;
    >
    > template <typename S, typename T>
    > void bar(void) {
    > typename map<S,T>::iterator theiter;
    >
    > }
    >
    > int main(void) {
    > return 0;
    >
    > }


    Thanks.

    So who isn't compliant, VC or gcc?
     
    Todd Anderson, Nov 2, 2005
    #4
  5. Todd A. Anderson

    red floyd Guest

    Todd Anderson wrote:
    > So who isn't compliant, VC or gcc?


    VC. Especially VC6.
     
    red floyd, Nov 2, 2005
    #5
  6. On Tue, 1 Nov 2005 17:56:04 -0800, "Todd A. Anderson"
    <0SPAM.net> wrote:

    >#include <map>
    >
    >using namespace std;
    >
    >template <typename S, typename T>
    >void bar(void) {
    > map<S,T>::iterator theiter;
    >}
    >
    >int main(void) {
    > return 0;
    >}
    >
    >At the declaration of theiter, gcc (both 3.4 and 4.0) gives me:
    >
    >error: expected ';' before "theiter"
    >
    >Am I totally braindead or why does this not work?
    >


    As others have pointed out, you need the keyword "typename". But I
    believe that gcc 3.3.2 used to issue a better error message (or was it
    merely a warning?) -- something like "implicit typename is
    deprecated...".

    --
    Bob Hairgrove
     
    Bob Hairgrove, Nov 2, 2005
    #6
  7. Todd A. Anderson

    Guest

    use:

    typename map<S,T>::iterator theiter;
     
    , Nov 2, 2005
    #7
    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. Allan Bruce

    To STL or not to STL

    Allan Bruce, Oct 16, 2003, in forum: C++
    Replies:
    41
    Views:
    1,055
    Christopher Benson-Manica
    Oct 17, 2003
  2. Replies:
    4
    Views:
    805
    Daniel T.
    Feb 16, 2006
  3. Replies:
    2
    Views:
    556
    klaus hoffmann
    Feb 22, 2006
  4. Replies:
    5
    Views:
    506
    Markus Schoder
    Apr 16, 2006
  5. Steve
    Replies:
    2
    Views:
    504
    Andre Kostur
    Nov 6, 2007
Loading...

Share This Page