2013-08-18 2 views
1

Это код, у меня есть:Класс не признается

template <class a, class b> 
class LinkedListIter{ 

    public: 
     LinkedListIter(b* iterable){ 

      this->iterable = iterable; 
      this->head = this->iterable->head; 
      this->curr = this->iterable->curr; 
      this->iter_pos = 0; 

     }; 
     void operator++(int dummy){ 

      this->iter_pos++; 

     }; 
     friend std::ostream& operator<< (std::ostream& o, LinkedListIter const& lli); 

    private: 
     a* head; 
     a* curr; 
     b* iterable; 
     int iter_pos; //The current position if the iterator 

    }; 

std::ostream& operator<< (std::ostream& o, LinkedListIter const& lli){ 
    return o << lli.get(lli.iter_pos); 
} 

Я получаю ошибку на линии, где я объявил о том, что std::ostream& operator<< (std::ostream& o, LinkedListIter const& lli) LinkedListIter не называет тип. Почему это так?

ответ

5

LinkedListIter - шаблон класса, а не класс. Поэтому ваш оператор также должен быть шаблоном.

template<typename a, typename b> 
std::ostream& operator<< (std::ostream& o, LinkedListIter<a,b> const & lli){ 
... 
+0

всего лишь секунду я попробую .. –

+0

yup, большое спасибо! –

+0

@AnshumanDwibhashi: Если у вас есть другой вопрос, лучше сделать отдельный пост. –

1
#include <iostream> 

template <class a, class b> 
class LinkedListIter; 

template <class a, class b> 
std::ostream& operator<< (std::ostream& o, LinkedListIter<a, b> const& lli); 

template <class a, class b> 
class LinkedListIter{ 
    public: 
     LinkedListIter(b* iterable) { 

      this->iterable = iterable; 
      this->head = this->iterable->head; 
      this->curr = this->iterable->curr; 
      this->iter_pos = 0; 

     }; 
     void operator++(int dummy) { 

      this->iter_pos++; 

     }; 
     friend std::ostream& operator<< <a, b> (std::ostream& o, LinkedListIter const& lli); 

    private: 
     a* head; 
     a* curr; 
     b* iterable; 
     int iter_pos; //The current position if the iterator 

}; 

template <class a, class b> 
std::ostream& operator<< (std::ostream& o, LinkedListIter<a, b> const& lli) { 
    return o << lli.get(lli.iter_pos); 
} 

этот код может составить хорошо, желание помочь.

+0

Спасибо за ваш ответ, это помогло, я поддержал ... –