CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    [RESOLVED] Template Horrors

    It seems all my problems involve templates. Anyways, my problem is whenever and wherever I put:
    Code:
    std::vector<T>::iterator iter;
    I get the compiler error:
    error: expected ';' before 'iter'

    I've tried putting the above code directly in a template method of a template class and directly as a member of the same class in it's definition. Each time I get the same error where it occures. I feel stupid because I think I'm missing something very fundamental here. Can anyone help? Thanks.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Template Horrors

    My guess is that you need to disambiguate with typename since the nested name iterator depends on T, which is presumably the template parameter. For example:
    Code:
    typename std::vector<T>::iterator iter;
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Template Horrors

    Would you mind elaborating a bit on this? I'm not sure what this will do.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  4. #4
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Template Horrors

    We need to see the bigger context in which that's being used. Is this inside another template class? etc... Post all relevant code.

    gg

  5. #5
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Template Horrors

    Well, I don't think much of it is very relevant, but here you go:
    Code:
    template<class T> class SomeClass
    {
    public:
        void someMethod (void)
        {
            ...
            std::vector<T>::iterator iter;   // I get the error everywhere. Whether I put it here...
            ...
        }
    protected:
        std::vector<T>::iterator iter;   // .. Or here, it doesn't matter
    };
    Edit:
    It looks like laserlight was right, because it will work now. It's just that I don't understand why I needed to do that.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Template Horrors

    Quote Originally Posted by Etherous
    It's just that I don't understand why I needed to do that.
    Because std::vector<T>::iterator can be interpreted as a non-type name (e.g., a variable name) or as a type name, and the rules say that it should be interpreted as a non-type name by default.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Template Horrors

    Quote Originally Posted by laserlight View Post
    Because std::vector<T>::iterator can be interpreted as a non-type name (e.g., a variable name) or as a type name, and the rules say that it should be interpreted as a non-type name by default.
    Okay, this is making more sense. For anyone else with this problem, I just stumbled on a good bit of reading that explains this very well:
    http://pages.cs.wisc.edu/~driscoll/typename.html
    Last edited by Etherous; March 21st, 2009 at 03:23 PM.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured