我有一个涉及嵌套命名空间和模板化类的问题。我还能够创建测试用例,它产生与实际代码相同的错误,但更具可读性。
使用 VS2012 和 2010 平台工具集编译以下代码会导致错误:
namespace A
{
namespace B
{
namespace C1
{
struct SMeasResult{};
}
namespace C2
{
struct SMeasResult{};
}
}
}
namespace C1Test
{
using namespace A::B::C1;
template<typename T>
class Fook
{
public:
void Yu()
{
SMeasResult Field;
}
};
}
namespace C2Test
{
using namespace A::B::C2;
template<typename T>
class Fook
{
public:
void Yu()
{
SMeasResult Field;
}
};
}
void m(){
C1Test::Fook<int> yu;
C2Test::Fook<int> me;
yu.Yu();
me.Yu();
}
具体错误如下:
1>------ Build started: Project: MultiVicomTest (Visual Studio 2010), Configuration: Debug Win32 ------
1> test.cpp
1>c:\code\test.cpp(27): warning C4101: 'Field' : unreferenced local variable
1> c:\code\test.cpp(26) : while compiling class template member function 'void C1Test::Fook<T>::Yu(void)'
1> with
1> [
1> T=int
1> ]
1> c:\code\test.cpp(49) : see reference to class template instantiation 'C1Test::Fook<T>' being compiled
1> with
1> [
1> T=int
1> ]
1>c:\code\test.cpp(43): error C2872: 'SMeasResult' : ambiguous symbol
1> could be 'c:\code\test.cpp(11) : A::B::C2::SMeasResult'
1> or 'c:\code\test.cpp(7) : A::B::C1::SMeasResult'
1> c:\code\test.cpp(42) : while compiling class template member function 'void C2Test::Fook<T>::Yu(void)'
1> with
1> [
1> T=int
1> ]
1> c:\code\test.cpp(50) : see reference to class template instantiation 'C2Test::Fook<T>' being compiled
1> with
1> [
1> T=int
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我不明白为什么符号“SMeasResult”对编译器来说是不明确的,因为它在单独的命名空间中使用。
到目前为止我发现的是,这个问题仅在类是模板类时出现。删除模板定义后,不会出现同样的问题。
谁能告诉我我是否做错了什么?
对我来说,这实际上看起来像是一个编译器错误。当您考虑到C1Test
编译的函数版本没有歧义,我怀疑以某种方式使用的命名空间namespace C1Test
甚至还徘徊在C2Test
命名空间。
g++ 4.4 和 4.5 都可以很好地编译此代码,这一事实进一步证实了这一点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)