Strange VC++ compile error, C2244(奇怪的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:奇怪的 VC++ 编译错误,C2244
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01