奇怪的 VC++ 编译错误,C2244 [英] Strange VC++ compile error, C2244

查看:24
本文介绍了奇怪的 VC++ 编译错误,C2244的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这段代码:

template <typename K,typename T>
Pointer<typename Collection<T>::Iterator> BinaryTree<K,T>::GetBeginning() const
{
    return new BinaryTreeIterator(this,BinaryTreeIterator::Position::atBeginning);
}

当我尝试使用 VSTS 2008 编译它时,我得到:

When I try to compile it using VSTS 2008, I get:

error C2244: 'BinaryTree<K,T>::GetBeginning' : unable to match function definition to an existing declaration
see declaration of 'BinaryTree<K,T>::GetBeginning'
2>        definition
2>        'Pointer<Collection<T>::Iterator> BinaryTree<K,T>::GetBeginning(void) const'
2>        existing declarations
2>        'Pointer<Collection<T>::Iterator> BinaryTree<K,T>::GetBeginning(void) const'

声明:

Pointer<Iterator> GetBeginning() const;

在班级内.BinaryTree间接继承自Collection,BinaryTreeIterator间接继承自Iterator,都是各自容器的嵌套类.

is inside the class. BinaryTree indirectly inherits from Collection, and BinaryTreeIterator indirectly inherits from Iterator, both nested classes of their respective containers.

您可以很容易地看到,即使在错误报告中,定义和声明也是相同的.这里真的有问题吗?

You can easily see that even in the error report, both definition and declaration are identical. Is there really something wrong here?

我发现微软发布了一个hotfix:某些模板代码无法编译,安装 Visual Studio 2005 Service Pack 1 后会出现错误 C2244".但是我找不到任何对 VSTS 2008 的引用.

I found that microsoft released a hotfix: "Certain template code does not compile, and error C2244 occurs after you install Visual Studio 2005 Service Pack 1". However I couldn't find any reference to VSTS 2008.

所以首先我想检查一下是否有人能一眼就发现代码中的真正错误,如果是 VS 的错,有没有人知道上面的修补程序是否是解决方案并且是否与 2008 相关.

So first I wanted to check if anybody could spot a real error in the code at a glance, and if it's VS's fault, does anyone know if the above hotfix is the solution and is relevant for 2008 as well.

推荐答案

对于那些有兴趣的人,我尝试编写了一个重现问题的最小示例:

For those interested, I tried writing a minimal sample reproducing the problem:

template <typename T>
struct Pointer {};

template <typename T>
struct Collection {
    struct Iterator {};
};

template <typename K,typename T>
struct BinaryTree : Collection<T>{
    Pointer<typename Collection<T>::Iterator> GetBeginning() const;

    struct BinaryTreeIterator : Collection<T>::Iterator {
        template <typename X>
        BinaryTreeIterator(BinaryTreeIterator*, X) {}
        struct Position {
            static int atBeginning() { return 0; }
        };
    };
};

template <typename K,typename T>
Pointer<typename Collection<T>::Iterator> BinaryTree<K,T>::GetBeginning() const
{
    return Pointer<typename Collection<T>::Iterator>();
}

int main(){
    BinaryTree<int, float> bt;
    bt.GetBeginning();
}

是的,我也收到错误消息.我在你的代码中看不到任何明显的错误,但话说回来,这个例子使用的嵌套类和继承比大多数理智的 C++ 程序员在一年内使用的更多,所以我不能肯定地说您的代码是否正确.

And yes, I get the error too. I can't see any obvious errors in what we've seen of your code, but then again, just this example has used more nested classes and inheritance than most sane C++ programmers do in a year, so I can't say for sure that your code is or isn't correct.

此外,我不得不猜测很多才能将其拼凑起来.(atBeginning 应该是什么?实际的类接口是什么?)

Moreover, I've had to guess quite a bit to piece this together. (What's atBeginning supposed to be? What are the actual class interfaces?)

但我怀疑如果您不从其他所有内容继承所有内容,它会工作得更好(并且更具可读性和更易于调试).

But I suspect it'd work better (and be more readable and easier to debug) if you didn't inherit everything from everything else.

更新我尝试用 GCC 和 Comeau 的在线编译器编译上面的内容,并且都接受了.所以看起来它可能是一个编译器错误.

Update I tried compiling the above with GCC and Comeau's online compiler, and both accepted it. So it seems like it could be a compiler bug.

这篇关于奇怪的 VC++ 编译错误,C2244的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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