我有两个模板类:
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:\github\pqueue\pqueue\pqueuecomp.h(163): error C2244: 'pQueueComp<D,V,Comp>::lowest': unable to match function definition to an existing declaration
1> d:\github\pqueue\pqueue\pqueuecomp.h(161): note: see declaration of 'pQueueComp<D,V,Comp>::lowest'
1> d:\github\pqueue\pqueue\pqueuecomp.h(163): note: definition
1> d:\github\pqueue\pqueue\pqueuecomp.h(163): note: 'pQueueNodeComp<D,V,Comp> *pQueueComp<D,V,Comp>::lowest(void)'
1> d:\github\pqueue\pqueue\pqueuecomp.h(163): note: existing declarations
1> d:\github\pqueue\pqueue\pqueuecomp.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>;
};
我遇到lowest
和add
的问题。
答案 0 :(得分:1)
以下编译并运行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的实际代码中,V
和D
模板参数是相反的