模板方法定义的问题 - 错误C2244:无法将函数定义与现有声明匹配
Issue with template method definition - error C2244: unable to match function definition to an existing declaration
我有两个模板类:
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>;
};
我有lowest
和add
的问题。
以下编译并使用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
模板参数反向
相关文章:
- 在命名空间中定义函数还是限定函数
- 为什么在定义函数之前先声明它
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 这个c++代码是如何在没有定义函数的情况下运行的
- 具有外部"c"和程序集的未定义函数
- 已定义函数时出现 G++ "未定义的引用"错误
- 将自定义函数传递到基抽象类中以延迟执行
- C++使用 rand 定义函数语法
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- 将具有固定签名的自定义函数名称注入 CRTP
- 使用定义函数模板别名
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 在内联程序集中定义函数和从 C++ 调用时出现问题
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- 声明和定义函数静态会产生"undefined reference to function_name()"
- 朋友定义函数的名称空间是什么
- 介子 对用户定义函数的未定义引用
- 如何使用 "using" 关键字定义函数原型/签名
- 在 C 结构中定义C++函数