首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套命名空间和歧义符号

嵌套命名空间和歧义符号
EN

Stack Overflow用户
提问于 2013-08-08 14:42:30
回答 2查看 1.7K关注 0票数 8

我遇到了一个问题,涉及嵌套的命名空间和模板化的类。我还能够创建测试用例,它产生与实际代码相同的错误,但更易读。

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

代码语言:javascript
复制
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();
}

具体错误如下:

代码语言:javascript
复制
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‘对编译器来说会很矛盾,因为它是在单独的名称空间中使用。到目前为止,我能发现的是,这个问题只在类是模板类时才出现。删除模板定义时不会出现相同的问题。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-08 14:55:54

在我看来,这实际上是一个编译器错误。当您考虑编译函数的C1Test版本时,我怀疑namespace C1Test中的使用名称空间在某种程度上甚至一直存在于C2Test命名空间中。

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

票数 3
EN

Stack Overflow用户

发布于 2013-08-08 15:05:18

尝试在您的C1Test和C2Test名称空间中使用A::B:C1::S测C1Test;以及使用A::B::C2::SMeasResult。这解决了这个问题。

顺便问一下,你为什么需要这么多的名称空间?STL非常大,但它只使用一个名称空间- std。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18128979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档