Thread: template class does not like list iterator

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    5

    template class does not like list iterator

    Here's a simple example that shows the error:
    Code:
    #include <list>
    using namespace std;
    template <class T>
    class fastlist {
      public:
      list<T>::iterator begin();
      private:
      list<T> elements;
    };
    And I get this error:
    Code:
    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
    Location
    Chch, NZ
    Posts
    597
    try
    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
    Posts
    5
    Thanks.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Quote Originally Posted by ChaosEngine View Post
    try
    typename list<T>::iterator::begin()
    Code:
    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
    Location
    Portland, OR
    Posts
    7,396
    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