嵌套的名称空间和不明确的符号 [英] Nested namespaces and ambiguous symbol

查看:143
本文介绍了嵌套的名称空间和不明确的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,那就是嵌套嵌套的名称空间和模板化类.我还能够创建测试用例,该用例会产生与实际代码相同的错误,但可读性更高.

I have a problem which involes nested namespaces and templated classes. I was also able to create test case which produces the same error as the actual code, but is a bit more readable.

使用VS2012和2010平台工具集编译以下代码会导致错误:

Compiling the following code using VS2012 with the 2010 platform toolset causes an error:

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"对于编译器会是模棱两可的,因为它在单独的命名空间中使用. 到目前为止,我能发现的是,仅当类是模板化类时才会出现此问题.删除模板定义时,不会出现相同的问题.

I do not understand why the symbol 'SMeasResult' would be ambigous to the compiler, as it is used in a separate namespace. What I could find out so far, is that this problem only appears when the classes are templated classes. The same problem does not appear when the template definition is removed.

有人可以告诉我我做错了什么吗吗吗?

Can anybody tell me if I did something wrong?

推荐答案

在我看来,这实际上是一个编译器错误.当您认为函数的C1Test版本编译明确时,我怀疑namespace C1Test中的using命名空间会一直徘徊在甚至进入C2Test命名空间的地方.

This actually looks like a compiler bug to me. When you consider that the C1Test version of the function compiled without ambiguity, I suspect that somehow the using namespace within namespace C1Test is lingering on even into the C2Test namespace.

g ++ 4.4和4.5都可以很好地编译此代码,这进一步证实了这一点.

This is further corroborated by the fact that g++ 4.4 and 4.5 can both compile this code fine.

这篇关于嵌套的名称空间和不明确的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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