C++ 尝试从另一个命名空间调用同名函数时,函数调用中的参数太少

C++ 尝试从另一个命名空间调用同名函数时,函数调用中的参数太少,c++,visual-c++,namespaces,C++,Visual C++,Namespaces,我在尝试从另一个名称空间调用函数时遇到了“函数调用中的参数太少”的问题,该名称空间恰好与调用它的函数同名。在这里,你可以明白我的意思: namespace doa::texture { using namespace internal::texture; Texture* const CreateTexture(const std::string& name, const std::string& pathToTextureImage) {

我在尝试从另一个名称空间调用函数时遇到了“函数调用中的参数太少”的问题,该名称空间恰好与调用它的函数同名。在这里,你可以明白我的意思:

namespace doa::texture {
   using namespace internal::texture;

      Texture* const CreateTexture(const std::string& name, const std::string& pathToTextureImage) {
         //below should call internal::texture::CreateTexture, not doa::texture::CreateTexture
         Texture* texture{ CreateTexture(pathToTextureImage) };
         ... //implementation detail
      }
}

namespace internal::texture {

   Texture* const CreateTexture(const std::string& pathToTextureImage) { ... }
}
我可以通过在函数调用前打一个
internal::texture
来轻松修复错误,但是由于我使用的是
usingnamespace internal::texture
指令,编译器应该能够拾取它

我还可以将
CreateTexture
函数从名称空间
internal::texture
移动到
doa::texture
。但我想避免这种情况

我如何在不移动函数或将
internal::texture
放在调用前面的情况下修复此问题,以及为什么会发生这种情况?这只是一个函数重载的例子,为什么名称空间会导致这样的事情发生?多谢各位

另外,这些功能在单独的文件中定义。像这样:

//irrelevant implementation details are left out
namespace doa::texture {

   Texture* const CreateTexture(const std::string& name, const std::string& pathToTextureImage);
}

namespace internal::texture {

   Texture* const CreateTexture(const std::string& pathToTextureImage);
}

我将尝试将C++17标准(草案)的几部分放在一起,希望我没有误解:)

从非限定名称查找中:

1在6.4.1中列出的所有情况下,按照各类别中列出的顺序搜索范围以获得声明;一旦找到名称的声明,名称查找就会结束。如果未找到声明,则程序的格式不正确

2 using指令指定的命名空间中的声明在包含using指令的命名空间中可见;见10.3.4。对于6.4.1中所述的非限定名称查找规则,来自using指令指定的名称空间的声明被视为该封闭名称空间的成员

从使用指令


2 using指令规定,指定名称空间中的名称可以在using指令出现在using指令之后的范围内使用。在非限定名称查找(6.4.1)过程中,名称似乎是在最近的封闭名称空间中声明的,该名称空间包含using指令和指定名称空间。[注:在此上下文中,“包含”表示“直接或间接包含”。-结束注]

从名字隐藏

1在嵌套的声明性区域或派生类(13.2)中,可以通过该名称的显式声明隐藏该名称


具体来说,对于最后一部分,我认为这意味着
doa::texture
中的
CreateTexture
函数隐藏
internal::texture
中的
CreateTexture
,即使它位于不同的名称空间中。基本上,编译器在
doa::texture
名称空间中看到
CreateTexture
,然后停止查找,因此忽略了
internal::texture

@ChrisMM中的一个,实际上,它在头文件中。让我编辑一下问题,让我把它弄清楚。你的代码受名称隐藏的影响。在
dao::texture::CreateTexture()
中,当您使用名称
CreateTexture
时。全名
dao::texture::CreateTexture
从其他名称空间隐藏名为
CreateTexture
的内容。使用命名空间internal::texture的
不会改变这一点。这意味着,在
dao::texture::CreateTexture()
中,您仍然需要提供
internal::texture::CreateTexture
的全名才能使用它。感谢您澄清@Peter。我在调用函数之前打了一个
internal::texture
,因为我不想更改它的名称。其他可能的修复方法包括更改函数名称或将函数移动到
doa::texture
。他们都工作。顺便问一下,您能否编辑您的评论,将
dao
s修改为
doa
s,Doğa是我的真名,我将其用作名称空间名称。“在不合格名称查找(6.4.1)过程中,名称看起来好像是在最近的封闭名称空间中声明的”我相信这意味着,使用
using
指令与将函数一起移动到
doa::texture
名称空间是一样的。如果是这种情况,所有这些都应该可以正常工作,因为在这种情况下,程序实际上编译并工作,
doa::texture::CreateTexture
不会隐藏
internal::texture::CreateTexture
。现在我更困惑了:D.我认为
名称隐藏
中的部分优先并结束查找。对不起,我修复了打印错误——我使用名为 DAO/CODE> >的命名空间,Lothmm,在这种情况下,C++不关心函数在查找名称声明时所需的参数。我得出这个结论是因为更改头文件中声明的顺序没有任何作用。我认为解决这个问题的唯一方法是在函数调用前键入
internal::texture
。谢谢你,我知道原因:P.S.DaO代表数据访问对象,或者别的什么东西?C++关心参数,但是一旦在同一个范围内声明了相同名称的东西,它就优先于上面的那些。这里有一个简化的例子:DAO是数据访问对象,用于DB工作。说到点子上了,谢谢你花时间@ChrisMM:)。