C ++中的命名空间重构

TeenyTinySparkles

我正在重构大型项目中一小部分的名称空间。因此,我重构了以下两个文件Oldname作为的命名空间x::y::z

file1.hpp

namespace x::y::z
{

Class abc
{
public:
       void funcA(type1 arg1, type2 arg2)   
}

file1.cpp

#include "file1.hpp"

namespace x::y::z
{
void abc::funcA(type1 arg1, type2 arg2)
{
--------
}
}

现在,我要解决此文件2,它与上面的文件具有依赖性。我不想更改此文件的名称空间,而只是修改依赖于上述文件的功能。

file2.cpp

#include "file1.hpp"

namespace Oldname
{
 void abc::funcA(type1 arg1, type2 arg2)
{
      mock("txt")
         .actualCall("abc.funcA")
         .withParameter("arg1", arg1)
         .withParameter("arg2", arg2)

}
}

我试图做到如下,

namespace Oldname
{
   void x::y::z::abc::funcA(type1 arg1, type2 arg2)
{
      mock("txt")
          .actualcall("abc.funcA")
          .withParameter("arg1",arg1)
          .withParameter("arg2",arg2)
}
}

但是,出现以下错误,

error: cannot define or redeclare 'funcA' here because namespace 'Oldname' does not enclose namespace 'abc'

有人可以帮助您解决此错误吗?

佳美

来自cppreference.com(添加了重点):

超出名称空间的定义和重新声明仅在声明之后才允许,仅在名称空间范围内,并且仅在包含原始名称空间(包括全局名称空间)的名称空间中使用,并且它们必须使用qualified-id语法

给定此规则,::x::y::z::abc::funcA允许的定义

  • 在类(::x::y::z::abc的定义中
  • 在命名空间::x::y::z
  • 在命名空间::x::y
  • 在命名空间中::x,或
  • 在全局名称空间中。

在任何其他命名空间中均不允许使用。特别是,它在namespace中是不允许的Oldname要解决此错误(没有引入namespace xfile2.cpp),把你定义出来Oldname,并把它放在全局命名空间。或者也许重新考虑如何设置名称空间。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章