VC ++模板编译器错误C2244:无法将函数定义与现有声明匹配 [英] VC++ Template Compiler Error C2244: Unable to Match Function Definition to an Existing Declaration

查看:360
本文介绍了VC ++模板编译器错误C2244:无法将函数定义与现有声明匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Visual Studio 2010遇到编译器错误,我将其简化为以下代码:

I've encountered a compiler error using Visual Studio 2010 which I've reduced down to the following code:

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 返回类型的模板参数绑定到<$ c内的静态成员 i $ c> B 而不是 B 的模板参数。当 i 的值相同时,为什么 F 的返回类型不同?这是一个错误吗?

If the declaration for i in struct B is removed the compiler error goes away. I believe it's because the template parameter for the return type of F is binding to the static member i within B instead of B's template argument. Why do the return types for F 'differ' when the value for i is the same? Is this a bug?

我还应该提到,如果将该函数声明为内联,错误就会消失。

I should also mention that if the function is declared inline the error goes away.

推荐答案

问题是您在同一范围内两次声明了相同的名称。如果您重命名静态const int i或模板参数,它将起作用。

The problem is you are declaring the same name twice in the same scope. If you rename the static const int i, or the template parameter it should work.

这篇关于VC ++模板编译器错误C2244:无法将函数定义与现有声明匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆