Templates and Iterators

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • card
    New Member
    • Jun 2007
    • 10

    Templates and Iterators

    Hi everyone,

    Can anybody explain why this won't build and what the error is? The error is with the list iterator in the TestClass class. Basically, I have a two templated classes, Item and TestClass. TestClass uses Item to define on of its template parameters.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include "test.h"
    
    using std::vector;
    using std::string;
    using std::list;
    
    template<class Key, class Element>
    class Item
    {
    private:
        Key _key;
        Element _element;
    };
    
    template<class Key, class Element, class Entry=Item<Key,Element> >
    class TestClass
    {
    private:
        list<Entry>::iterator  myIter;
    };
    
    int main(int argc, char *argv[])
    {
        vector< list<TestClass<string, int> > > _v;
        list<TestClass<string, int> >::iterator _it;
     
        return 0;
    }
    The build error using g++ 4.2.3 is:

    test.C:33: error: type ‘std::list<Entr y, std::allocator< _Tp> >’ is not derived from type ‘TestClass <Key, Element, Entry>’
    test.C:33: error: expected ‘;’ before ‘myIter’

    I can't figure this one out. Many thanks.

    -David
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    I have no idea if it is working, but at least it compiles ( when i removed include "test.h" line )
    Code:
    typename list<Entry>::iterator  myIter;

    Comment

    • card
      New Member
      • Jun 2007
      • 10

      #3
      Yeah, sorry about leaving the "test.h" include in there. Thanks for the reply. Works great.

      Comment

      Working...