【发布时间】:2019-12-01 00:07:48
【问题描述】:
我在尝试从另一个命名空间调用函数时遇到“函数调用中的参数太少”,而该命名空间恰好与调用它的函数同名。在这里,你可以明白我的意思:
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 指令,编译器应该能够识别它。
我还可以将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);
}
【问题讨论】:
-
@ChrisMM 实际上,它在头文件中。让我编辑问题以使其清楚。
-
您的代码受到名称隐藏的影响。在
dao::texture::CreateTexture()内,当您使用名称CreateTexture时。全名dao::texture::CreateTexture对其他命名空间隐藏了名为CreateTexture的事物。using namespace internal::texture不会改变这一点。这意味着,在dao::texture::CreateTexture()内,您仍然需要提供internal::texture::CreateTexture的全名才能使用它。 -
感谢您澄清@Peter。我在调用该函数之前打了一个
internal::texture,因为我不想更改它的名称。其他可能的修复包括更改函数的名称或将函数移动到doa::texture。他们都工作。顺便说一句,您可以编辑您的评论以将daos 修复为doas,Doğa 是我的真实姓名,我将其用作命名空间名称。
标签: c++ visual-c++ namespaces