Thread: template class does not like list iterator

  1. #1
    Registered User
    Join Date
    Jun 2007

    template class does not like list iterator

    Here's a simple example that shows the error:
    #include <list>
    using namespace std;
    template <class T>
    class fastlist {
      list<T>::iterator begin();
      list<T> elements;
    And I get this error:
    fastlist.h:6: error: expected `;' before "begin"
    It compiles fine when the member function is list<int>::iterator begin(), but I want begin() to return a list iterator for a list of class T.

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Chch, NZ
    typename list<T>::iterator::begin()
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Jun 2007

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Quote Originally Posted by ChaosEngine View Post
    typename list<T>::iterator::begin()
    typename list<T>::iterator begin();
    Because he was declaring a function (no :: before begin). But yes, typename did it. Damned semantic ambiguities.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    The general rule is if you are trying to specify a fully qualified type name, AND the type name contains a template parameter (either implicitly or explicitly), AND the typename contains a scoping "::" operator, you need to put "typename" in front of the type.

    You even have to do it in contexts where the fully qualified name could not be anything BUT a type, which is irritating (e.g. in the return type of a function, like in this case).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. help with template class using a template node
    By aciarlillo in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2005, 05:46 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 08:49 PM