模板方法定义的问题 - 错误C2244:无法将函数定义与现有声明匹配

Issue with template method definition - error C2244: unable to match function definition to an existing declaration

本文关键字:定义 函数 声明 问题 错误 C2244 模板方法      更新时间:2023-10-16

我有两个模板类:

template<typename D, typename V, typename Comp>
class pQueueNodeComp;

和:

template <typename D, typename V, typename Comp>
class pQueueComp;

pQueueComp中,我有一种方法为:

pQueueNodeComp<D, V,Comp>* lowest();

这是定义:

template <typename D, typename V, typename Comp>
pQueueNodeComp<D, V, Comp>* pQueueComp<D, V, Comp>::lowest() {
    return binaryHeap[0]; //binaryHeap is a vector<pQueueNodeComp<D, V, Comp>*>
}

我在Visual Studio 2015上遇到以下错误:

1>d:githubpqueuepqueuepqueuecomp.h(163): error C2244: 'pQueueComp<D,V,Comp>::lowest': unable to match function definition to an existing declaration
1>  d:githubpqueuepqueuepqueuecomp.h(161): note: see declaration of 'pQueueComp<D,V,Comp>::lowest'
1>  d:githubpqueuepqueuepqueuecomp.h(163): note: definition
1>  d:githubpqueuepqueuepqueuecomp.h(163): note: 'pQueueNodeComp<D,V,Comp> *pQueueComp<D,V,Comp>::lowest(void)'
1>  d:githubpqueuepqueuepqueuecomp.h(163): note: existing declarations
1>  d:githubpqueuepqueuepqueuecomp.h(163): note: 'pQueueNodeComp<D,V,Comp> *pQueueComp<D,V,Comp>::lowest(void)'

在我看来,声明与定义匹配。我要疯了吗?

编辑:类和方法的定义在同一文件中。

edit2:这是pQueueComp的完整定义:

template <typename V, typename D, typename Comp>
class pQueueComp {
public:
    pQueueComp(Comp _cmp) :
        cmp(_cmp)
    {};
    pQueueNodeComp<D, V,Comp>* add(const D& data, V value);
    pQueueNodeComp<D, V,Comp>* lowest();

    void removeLowest();
    int size() { return binaryHeap.size(); };
    ~pQueueComp();
    pQueueComp() {};
    pQueueComp(const pQueueComp&) = delete; /*out of the scope of this project*/
    pQueueComp& operator=(const pQueueComp&) = delete;
    void print();
private:
    Comp cmp;
    std::vector<pQueueNodeComp<D, V, Comp>*> binaryHeap;
    void changeValue(int rank, V newValue);

    void goDown(int rank);
    void goUp(int rank);
    void swap(int rank1, int rank2);
    int parent(int i) { return (i + 1) / 2 - 1; };
    int child1(int i) { return 2 * (i + 1) - 1; }
    int child2(int i) { return 2 * (i + 1); }
    friend class pQueueNodeComp<D, V, Comp>;
};

我有lowestadd的问题。

以下编译并使用VS15:运行header.h:

template <typename D, typename V, typename Comp>
class pQueueNodeComp
{
    D d;
    V v;
    Comp c;
};

template <typename D, typename V, typename Comp>
class pQueueComp
{
public:
    pQueueNodeComp<D, V, Comp>* lowest();
};
template<typename D, typename V, typename Comp>
pQueueNodeComp<D, V, Comp>* pQueueComp<D, V, Comp>::lowest()
{
    return nullptr;
}

main.cpp:

#include "Header.h"
int main()
{
    pQueueComp<int, int, int> x;
    auto y = x.lowest();
    return 0;
}

编辑:我在查看您的Edit2之前发布了发布。请注意,在您的第一个示例中,模板类型是一个顺序的,在Pqueuecomp的实际代码中,VD模板参数反向