C++模板类迭代器错误

标签 c++ templates iterator

我正在尝试使用模板类为基于链表的自定义容器实现迭代器类。我正在尝试遍历链表中的节点。

我的主要代码是:

#include "Smaph.h"
#include <iostream>
#include <string>
using namespace std;

int main() {
Smaph<string, int> x1;
x1.insert("john", 3);
x1.insert("alex", 5);
cout << "Size is " << x1.size() << endl;
Smaph<string, int>::iterator it;
for (it=x1.begin(); it!=x1.end(); ++it)
    cout << *it << endl;
}

错误发生在for循环语句执行时,调用x1.begin()

错误是在转换头指针时:

could not convert
((const Smaph<std:basic_string<char>, int>*)this)->
    Smaph<std::basic_string<char>, int>::head
from
    Smaph<std::basic_string<char>, int>::node* const
to
    Smaph<std::basic_string<char>, int>::iterator
    aka sl_iterator<std::basic_string<char>, int>

以下是我的单个Smaph.h文件

template <typename T1, typename T2> class sl_iterator;

template <typename T1, typename T2>
class Smaph
{
public:
    typedef T1 key_type;
    typedef T2 mapped_type;
    typedef unsigned int size_type;
    typedef sl_iterator<T1, T2> iterator;

friend class sl_iterator<T1, T2>;

struct node {
    T1 datum1;
    T2 datum2;
    struct node *next;
};
node *head, *tail;
Smaph() : head(0), tail(0)  { }
~Smaph() { clear(); }

bool insert(const key_type &first, const mapped_type &second) {
    node *p = new node;
    p->datum1 = first;
    p->datum2 = second;
    p->next = 0;
    if (!tail)              // empty list?
        head = p;
    else
        tail->next = p;
    tail = p;
    return (1);     // return true for now
}

size_type size() const {
    int count=0;
    for (node *p = head; p; p=p->next)
        count++;
    return count;
}

void clear() {
    while (head) {
        node *p = head->next;
        delete head;
        head = p;
        }
}

bool empty() const {
    return !head;
}

iterator begin() const {
    return head;
}

iterator end() const {
    return 0;
}
};

template <typename T1, typename T2>
class sl_iterator {
public:
    typedef T1 key_type;
    typedef T2 mapped_type;
    typedef unsigned int size_type;

    struct node {
        T1 datum1;
        T2 datum2;
        struct node *next;
    };

//private:
    node *p;
    // This private ctor is for the container class only:
    sl_iterator(node *ptr) : p(ptr) { }
public:
    sl_iterator() : p(0) { }
    sl_iterator &operator++() {        // Preincrement
        p = p->next;
        return *this;
    }
    sl_iterator operator++(int) {      // Postincrement
        const sl_iterator tmp = *this;
        ++*this;
        return tmp;
    }

    // *sl_iterator: Return a reference to the datum
    T1 &operator*() const {
        return p->datum1;
    }
    // sl_iterator->: Return the address of the datum
    T1 *operator->() const {
        return &p->datum1;
    }

    bool operator==(const sl_iterator &rhs) const {
        return p==rhs.p;
    }
    bool operator!=(const sl_iterator &rhs) const {
        return !(*this == rhs);
    }
}; // end class 

最佳答案

您似乎希望嵌套类 sl_iterator<T1, T2>::nodeSmaph<T1, T2>::node将被编译器视为同一个类。尽管它们的定义相同,但情况并非如此。

您可能想要更改 sl_iterator 的定义因此它不包含 node 的进一步定义,而是指 Smaph::node :

template <typename T1, typename T2>
class sl_iterator {
// ...
    typename Smaph<T1, T2>::node *p;
    sl_iterator(typename Smaph<T1, T2>::node *ptr) : p(ptr) { }

// ...
};

关于C++模板类迭代器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16264745/

相关文章:

java - C++ 命名空间,与 Java 包的比较

javascript - JS改变倒计时计数器

推断类成员的 C++ 模板

C++ vector 插入和迭代器混淆

c++ - 为 Windows VS2013 构建 Boost 正则表达式

c++ - C++中的简单继承

templates - 使用nvcc在CUDA中编译模板函数时出错

java - 为什么在 HashMap 迭代期间,更改键/值对的值不会抛出 ConcurrentModificationException?

c++ - 函数返回基类而不是派生类,这是编码错误还是 Visual C++ 错误?

c++ - 错误 : Invalid options syntax: -//tensorflow:libtensorflow_cc. 所以