错误代码,在用std::distance(begin(), end());函数是出错,实在是搞不懂,请教!!!!!!!!!
```1>c:\users\taoyanqi\desktop\tictactoe-ai-master\datastruct\datastruct\mylist.h(122): error C2893: 未能使函数模板“iterator_traits<_Iter>::difference_type std::distance(_InIt,_InIt)”专用化
1> c:\users\taoyanqi\desktop\tictactoe-ai-master\datastruct\datastruct\mylist.h(122): note: 用下列模板参数:
1> c:\users\taoyanqi\desktop\tictactoe-ai-master\datastruct\datastruct\mylist.h(122): note: “_InIt=_list_iterator”
1> c:\users\taoyanqi\desktop\tictactoe-ai-master\datastruct\datastruct\mylist.h(120): note: 编译类 模板 成员函数“myList::size_type myList::size(void)”时
1> c:\users\taoyanqi\desktop\tictactoe-ai-master\datastruct\datastruct\mylist_demo.cpp(7): note: 参见对正在编译的函数 模板 实例化“myList::size_type myList::size(void)”的引用
1> c:\users\taoyanqi\desktop\tictactoe-ai-master\datastruct\datastruct\mylist_demo.cpp(5): note: 参见对正在编译的类 模板 实例化“myList”的引用
未能使函数模板“iterator_traits<_Iter>::difference_type std::distance(_InIt,_InIt)”专用化??????????????
begin和end函数返回的类型不明确。
你可以强转转换成具体类型,比如你的迭代器是 vector<某类型>,那么可以用
std::distance((std::vector<莫类型>::iterator)begin(), (std::vector<某类型>::iterator)end());
#ifndef MY_LIST_H
#define MY_LIST_H
#include <memory>
#include <iostream>
#include <algorithm>
//define the list node
template<class T>
struct _list_node {
T data;
_list_node<T> *prev;
_list_node<T> *next;
};
//lise iterator
template<class T>
struct _list_iterator{
typedef _list_iterator<T> iterator;
typedef _list_iterator<T> self;
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type; //表示两个迭代器之间的距离 ,c++内置定义 typedef int ptrdiff_t;
typedef T* pointer;
typedef T& reference;
typedef _list_node<T>* link_type;
link_type node; //迭代器内部的普通指针,指向list的节点
_list_iterator(link_type x) : node(x) {} //构造函数
_list_iterator() {}
_list_iterator(const iterator& x) : node(x.node) {}
bool operator==(const self& x)const { return node == x.node; }
bool operator!=(const self& x)const { return node != x.node; }
reference operator*()const { return (*node).data; } //对迭代器取值,解引用
pointer operator->()const { return &(operator*()); } //重载->运算,对迭代器的成员存取
self& operator++() {
node = (link_type)((*node).next); //迭代器累加,获得下一个节点
return *this; //注意这里要返回一个this指针
}
self operator++(int) {
self tmp = *this;
++*this;
return tmp;
}
self& operator--() {
node = (link_type)((*node).next);
return *this;
}
self operator--(int) {
self tmp = *this;
--*this;
return tmp;
}
};
//list定义
template <class T> class myList
{
protected:
typedef _list_node<T> list_node;
typedef list_node* link_type;
link_type node;
//typedef std::allocator<list_node> _alloc;
typedef std::allocator<list_node> list_node_allocator;
list_node_allocator _alloc;
public:
template<class T, class Ref, class Ptr>
friend struct _iterator;
typedef T value_type;
typedef size_t size_type;
typedef T& reference;
typedef ptrdiff_t difference_type;
typedef _list_iterator<T> iterator;
iterator begin() { return (link_type)((*node).next); }
iterator end() { return node; } //返回空的节点,满足了前闭后开原则
iterator head;
iterator tail;
bool empty()const { return head == tail; } //因为是环状的,所以可以判断为空条件
size_type size();
reference front() { return *begin(); }
reference back() { return *(--end()); }
protected:
link_type get_node() { return _alloc.allocate(1); }
void put_node(link_type p) { list_node_allocator::deallocate(p) }
public:
myList() { empty_initialize(); };
protected:
void empty_initialize(); //定义空的链表
};
//
template <class T>
void myList<T>::empty_initialize() {
node = get_node(); //get_node是一个
node->next = node;
node->prev = node;
//node->data = 0;
}
template <class T>
typename myList<T>::size_type myList<T>::size() {
difference_type result = 0;
std::distance(iterator(begin()),iterator( end()));
return result;
}
#endif