模板构造函数没有匹配的函数调用错误

时间:2013-06-15 18:20:42

标签: c++ templates

以下是类声明的摘录:

template<typename key_type,typename value_type>
class _IndexTupleTree_Iterator
{
public:

    typedef typename std::vector<value_type>::iterator value_it_t;
    _IndexTupleTree_Iterator(std::vector<GenNode<key_type,value_type>*>& node_stack,std::vector<int>& key_pos_stack,value_it_t value_pos);
 ...
 }

我尝试在下面的上下文中调用上面的构造函数(最后一行):

template<typename key_type,typename value_type>
class LeafNode : public GenNode<key_type,value_type>
{
public:
    typedef std::pair< std::vector<key_type>,value_type > entry_t;
    LeafNode(const entry_t entry);
    void insert(const entry_t entry);
    _IndexTupleTree_Iterator<key_type,value_type> begin(std::vector<GenNode<key_type,value_type>*>& node_stack,std::vector<int>& key_pos_stack) const;
    bool operator==(const LeafNode<key_type,value_type>& rhs) const;
    //bool find(std::vector<key_type> key, _IndexTupleTree_Iterator& pos);
private:
    std::vector<value_type> _values;
};

 template<typename key_type,typename value_type>
_IndexTupleTree_Iterator<key_type,value_type> LeafNode<key_type,value_type>::begin(std::vector<GenNode<key_type,value_type>*>& node_stack,std::vector<int>& key_pos_stack) const
{
    key_pos_stack.push_back(0);
    return _IndexTupleTree_Iterator<key_type,value_type>(node_stack,key_pos_stack, _values.begin());
}

但是我收到以下错误:

../src/tensor_utils/IndexTupleTree.h:134: error: no matching function for call to ‘_IndexTupleTree_Iterator<int, int>::_IndexTupleTree_Iterator(std::vector<GenNode<int, int>*, std::allocator<GenNode<int, int>*> >&, std::vector<int, std::allocator<int> >&, __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >)’
../src/tensor_utils/IndexTupleTree.h:55: note: candidates are: _IndexTupleTree_Iterator<key_type, value_type>::_IndexTupleTree_Iterator(std::vector<GenNode<key_type, value_type>*, std::allocator<GenNode<key_type, value_type>*> >&, std::vector<int, std::allocator<int> >&, typename std::vector<value_type, std::allocator<_T2> >::iterator) [with key_type = int, value_type = int]

我知道问题出在最后一个论点上。有谁知道它为什么找不到构造函数并调用它?

1 个答案:

答案 0 :(得分:3)

  

有谁知道它为什么找不到构造函数并调用它?

这是因为您的begin()功能为const - 符合条件,这意味着执行_values.begin()会返回const_iterator

但是,_IndexTupleTree_Iterator的构造函数接受非const iterator作为最后一个参数。这就是编译器无法解析调用的原因。