std::map Iterator in Template



  • Hallo,

    ich versuche gerade ein Manager-Template zu schreiben, wo man verschiedene Klasseninstanzen(pointer) speichern kann und alle eine ID bekommen. Leider will bei mir der Iterator im Template nicht funktionieren (in main() funktioniert es hingegen problemlos) und ich weiß überhaupt nicht, woran das liegt.

    Hier der Code (Fehler siehe Zeile 39):

    #include <iostream>
    #include <map>
    
    template<class T>
    class Manager
    {
    	private:
    		std::map<unsigned int, T*> t;
    		unsigned int id;
    
    	protected:
    
    	public:
    		Manager(){ id = 0; }
    		~Manager(){ if(!t.empty()) t.clear(); }
    
    		unsigned int Add(T* obj);
    		void foobar();
    		T* GetObject(const unsigned int& id);
    };
    
    template<class T>
    unsigned int Manager<T>::Add(T* obj)
    {
    	t[++id] = obj;
    	return id;
    }
    
    template<class T>
    T* Manager<T>::GetObject(const unsigned int& id)
    {
    	if(t.find(id) != t.end()) return t.find(id)->second;
    	else return 0;
    }
    
    template<class T>
    void Manager<T>::foobar()
    {
    	std::map<unsigned int, T*>::iterator it; [b]// ERROR: expected ; before it[/b]
    
    	for(it = t.begin(); it != t.end(); ++it)
    		std::cout << it->second->foobar() << std::endl;
    }
    
    class Auto
    {
          public:
                 Auto(){}
                 ~Auto(){}
    
                 int foo(){return bar;}
                 void foo(int k){bar = k;}
          private:
                  int bar;
    };
    
    int main ()
    {
      Auto* autoA = new Auto(); autoA->foo(12);
      Auto* autoB = new Auto(); autoB->foo(23);
      Auto* autoC = new Auto(); autoC->foo(52);
    
      Manager<Auto> m;
      m.Add(autoA);
      m.Add(autoB);
      m.Add(autoC);
    
      m.foobar();
    
      std::cin.peek();
      return 0;
    }
    

    MfG



  • typename std::map<unsigned int, T*>::iterator it;

    solle das problem loesen

    siehe http://www.cs.wisc.edu/~driscoll/typename.html



  • Danke für die schnelle Hilfe 🙂


Anmelden zum Antworten