【问题标题】:unable to match function definition to an existing declaration无法将函数定义与现有声明匹配
【发布时间】:2011-12-02 21:49:26
【问题描述】:

在尝试向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 超图::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 问题,由指针容器引起,但我将在单独的问题中解决这个问题。

【问题讨论】:

    标签: c++ templates function


    【解决方案1】:

    返回值:

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

    应该是:

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

    【讨论】:

    • 天哪……我真的只是……哇。并且没有“错误类型”警告,因为它是一个模板,所以它不知道类型......哇
    猜你喜欢
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2021-11-01
    相关资源
    最近更新 更多