VC++模板编译器错误C2244:无法将函数定义与现有声明匹配
VC++ Template Compiler Error C2244: Unable to Match Function Definition to an Existing Declaration
我在使用Visual Studio 2010时遇到了一个编译器错误,我将其简化为以下代码:
template <int i> struct A
{
typedef int T;
};
template<int i>
struct B
{
static const int i = i; // <-- this seems to cause the problem
typename A<i>::T F();
};
template<int i>
typename A<i>::T B<i>::F() { return B<i>::i; }
此代码产生以下错误:
repro.cpp(15): error C2244: 'B<i>::F' : unable to match function definition to an existing declaration
repro.cpp(12) : see declaration of 'B<i>::F'
definition
'A<i>::T B<i>::F(void)'
existing declarations
'A<i>::T B<i>::F(void)'
如果删除了结构B
中i
的声明,编译器错误就会消失。我认为这是因为F
的返回类型的模板参数绑定到B
中的静态成员i
,而不是B
的模板参数。当i
的值相同时,为什么F
的返回类型"不同"?这是个虫子吗?
我还应该提到,如果函数是内联声明的,那么错误就会消失。
问题是在同一范围内两次声明相同的名称。如果您重命名静态常量int i或模板参数,它应该可以工作。
相关文章:
- 在命名空间中定义函数还是限定函数
- 为什么在定义函数之前先声明它
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 这个c++代码是如何在没有定义函数的情况下运行的
- 具有外部"c"和程序集的未定义函数
- 已定义函数时出现 G++ "未定义的引用"错误
- 将自定义函数传递到基抽象类中以延迟执行
- C++使用 rand 定义函数语法
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- 将具有固定签名的自定义函数名称注入 CRTP
- 使用定义函数模板别名
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 在内联程序集中定义函数和从 C++ 调用时出现问题
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- 声明和定义函数静态会产生"undefined reference to function_name()"
- 朋友定义函数的名称空间是什么
- 介子 对用户定义函数的未定义引用
- 如何使用 "using" 关键字定义函数原型/签名
- 在 C 结构中定义C++函数