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

查看:169
本文介绍了奇怪的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?

我发现微软发布了一个修补程序:某些模板代码无法编译,并且在安装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.

此外,我不得不猜测一下。 (

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天全站免登陆