尝试从另一个名称空间调用具有相同名称的函数时,函数调用中的参数太少 [英] Too few arguments in function call while trying to call function with the same name from another namespace

查看:424
本文介绍了尝试从另一个名称空间调用具有相同名称的函数时,函数调用中的参数太少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试从另一个名称空间恰好与要从其调用的函数同名的另一个命名空间调用函数时,出现函数调用中的参数太少"的情况.在这里,您可以了解我的意思:

I'm getting a "too few arguments in function call" while trying to call a function from another namespace that happens to have the same name with the function it's being called from. Here, you can see what I mean:

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来轻松修复该错误,但是由于我使用的是using namespace internal::texture指令,因此编译器应该可以将其提取.

I can easily fix the error by slapping an internal::texture in front of the function call but since I'm using the using namespace internal::texture directive, the compiler should be able to pick it up.

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

I can also move the CreateTexture function from the namespace internal::texture to doa::texture. But I would like to avoid that.

如何在不移动功能或将internal::texture置于调用前面的情况下解决此问题,为什么首先发生这种情况?这只是函数重载的情况,为什么命名空间会导致这种情况发生?谢谢.

How can I fix this without moving the function or putting internal::texture in front of the call, and why this thing happens in the first place? This is simply a case of function overloading, why do namespaces cause such a thing to happen? Thank you.

P.S.这些功能在单独的文件中定义.像这样:

P.S. The functions are defined in a separate file. like this:

//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标准(草稿)放在一起,并希望我不要误解它:)

I'm going to try to put a few pieces of the C++17 standard (draft) together, and hope I'm not misinterpretting it :)

通过非限定名称查找:

1在6.4.1中列出的所有情况下,都将在范围中搜索各个类别中列出的顺序的声明;找到名称声明后,名称查找将立即结束.如果找不到声明,则说明程序格式错误.

1 In all the cases listed in 6.4.1, the scopes are searched for a declaration in the order listed in each of the respective categories; name lookup ends as soon as a declaration is found for the name. If no declaration is found, the program is ill-formed.

2由using-directive提名的命名空间中的声明在包含using-directive的命名空间中变得可见.见10.3.4.出于6.4.1中描述的不合格名称查找规则的目的,使用指令指定的命名空间声明被视为该封闭命名空间的成员.

2 The declarations from the namespace nominated by a using-directive become visible in a namespace enclosing the using-directive; see 10.3.4. For the purpose of the unqualified name lookup rules described in 6.4.1, the declarations from the namespace nominated by the using-directive are considered members of that enclosing namespace.

使用指令

2 using伪指令指定可以在使用伪指令之后出现using伪指令的范围内使用指定命名空间中的名称.在无限制的名称查找(6.4.1)中,这些名称看起来像是在最近的包含命名空间的封闭名称空间中声明的,该名称空间同时包含using指令和指定的名称空间. [注:在本文中,包含"是指直接或间接包含". —尾注]

2 A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup (6.4.1), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. [Note: In this context, "contains" means "contains directly or indirectly". —end note]

隐藏姓名

1名称可以通过在嵌套的声明性区域或派生类(13.2)中使用相同名称的显式声明来隐藏.

1 A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class (13.2).

具体地说,在最后一部分中,我相信这意味着doa::texture中的CreateTexture函数将CreateTexture隐藏在internal::texture中,即使它在另一个命名空间中.基本上,编译器会在doa::texture命名空间中看到CreateTexture,然后停止查找,因此忽略了internal::texture

Specifically, with this last part, I believe it means that the CreateTexture function in doa::texture hides the CreateTexture in internal::texture even though it is within a different namespace. Basically, the compiler sees CreateTexture in the doa::texture namespace, then stops looking, and hence ignored the one from internal::texture

这篇关于尝试从另一个名称空间调用具有相同名称的函数时,函数调用中的参数太少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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