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 没有命名类型的那一行出现错误。为什么会这样?

4

2 回答 2

5

LinkedListIter是类模板,而不是类。所以你的运营商也需要是一个模板。

template<typename a, typename b>
std::ostream& operator<< (std::ostream& o, LinkedListIter<a,b> const & lli){
...
于 2013-08-18T03:27:51.740 回答
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); 
}

此代码可以很好地编译,希望有所帮助。

于 2013-08-18T03:53:50.590 回答