无法将函数定义与现有声明匹配

时间:2011-12-02 21:49:26

标签: c++ templates function

在尝试向this question提供修改时,我遇到了错误。对于我的8个函数,我遇到了错误:

  

f:\ code \ utilities \ hypergraph \ hypergraph \ hypergraph.h(233):错误C2244:'hypergraph :: add_edge':无法将函数定义与现有声明匹配
           f:\ code \ utilities \ hypergraph \ hypergraph \ hypergraph.h(68):参见'hypergraph :: add_edge'的声明
           定义
           'hypergraph :: node_iter hypergraph :: add_edge(void)'
           现有声明
           'std :: set :: node *,ptr_cmp :: node,P>,A> :: const_iterator hypergraph :: add_edge(void)'

这个班级:

template<class T, class P>
struct ptr_cmp 
    : public std::binary_function<T, T, bool> {
    P p_;
    ptr_cmp(P p=P()) :p_(p) {}
    bool operator()(const T* l, const T* r) const
    { return p_(*l, *r);}
};

template<class T, class P = std::less<T>, class A=std::allocator<T> >
class hypergraph {
    typedef A sub_allocator;
public:
    class node;
    class edge;
    typedef std::set<edge*, ptr_cmp<edge, std::less<edge> >, sub_allocator> edgeset;
    typedef std::set<node*, ptr_cmp<node, P>, sub_allocator> nodeset;
    typedef typename std::set<edge*, ptr_cmp<edge, std::less<edge> >, sub_allocator>::const_iterator edgeiter;
    typedef typename std::set<node*, ptr_cmp<node, P>, sub_allocator>::const_iterator nodeiter;

    class node { /*SNIP*/};
    class edge { /*SNIP*/};

    hypergraph(P pred=P(), A alloc=A());

    nodeiter add_node(); /* beginning of 8 with the error */
    nodeiter add_node(const T& rhs);
    nodeiter add_node(T&& rhs);
    nodeiter add_edge();
    nodeiter erase(nodeiter iter);
    nodeiter erase(node* iter);
    nodeiter erase(edgeiter iter);
    nodeiter erase(edge* iter);  /* end of 8 with the error */

    const nodeset& nodes() const;
    const edgeset& edges() const;

    A get_allocator() const;
protected:
    hypergraph(const hypergraph& rhs);
    hypergraph& operator=(const hypergraph& rhs);
    A a_;
    nodeset nodes_;
    edgeset edges_;
    unsigned int edgecount_;
};

这个功能定义:

template<class T, class P, class A>
typename hypergraph<T,P,A>::node_iter hypergraph<T,P,A>::add_edge()
{
    std::unique_ptr<edge> ptr = new edge(edgecount_++, sub_allocator(a_));
    std::pair<edgeiter, bool> r = edges_.insert(ptr);
    ptr.release();
    return r.first;
}

我确定这是一个愚蠢的事情,但我无法弄清楚为什么MSVC10无法将该原型与该功能相匹配。

我认为这段代码存在const-ness问题,这是由指针容器引起的,但我会在一个单独的问题中解决这个问题。

1 个答案:

答案 0 :(得分:2)

返回值:

typename hypergraph<T,P,A>::node_iter

应该是:

typename hypergraph<T,P,A>::nodeiter