简体   繁体   English

为什么类方法不能使用相同的名称调用全局函数?

[英]Why can't a class method call a global function with the same name?

The following code shows a function call another function. 以下代码显示了一个函数调用另一个函数。
Both have the same name, but different signatures. 两者名称相同,但签名不同。
This works as expected. 这按预期工作。

//declarations
void foo();
void foo(int);

int main(){
  foo();
}

//definitions
void foo(){
    foo(1);
}
void foo(int){}

The only difference I will make now, is wrap one of the functions into a structure: 我现在唯一要做的就是将其中一个函数包装到一个结构中:

//declarations
struct Bar{
    void foo();
};
void foo(int);

int main(){
  Bar bar;
  bar.foo();
}

//definitions
void Bar::foo(){
    foo(1);
}
void foo(int){}

This fails to compile. 这无法编译。

In member function ‘void Bar::foo()’:
error: no matching function for call to ‘Bar::foo(int)’
         foo(1);
              ^
note: candidate: void Bar::foo()
     void Bar::foo(){
          ^
note:   candidate expects 0 arguments, 1 provided

I don't understand why it wants to call foo(int) as a method, when the global function exists. 当全局函数存在时,我不明白为什么它要调用foo(int)作为方法。
It doesn't mention anything about ambiguity, it just can't find the function. 它没有提及任何歧义,只是找不到函数。

Why is this happening, and how can I fix it? 为什么会发生这种情况,我该如何解决?

side note: I'm wrapping old C code in a C++ wrapper, and most of the C++ methods are calls to the global C functions which pass in the wrapped struct implicitly. 旁注:我将旧的C代码包装在C ++包装器中,并且大多数C ++方法都是对全局C函数的调用,这些函数隐式地传递包装的结构。 It's a similar situation to what is happening above (in terms of compiler errors). 这与上面发生的情况类似(就编译器错误而言)。

The member function is hiding the global. 成员函数隐藏全局。 It finds the name in the class context, therefore it not continue to search it in other contexts. 它在类上下文中找到该名称,因此不会在其他上下文中继续搜索它。

You need to call it like this: 您需要这样称呼它:

::foo(1);

Another solution is to use forward declaration inside the function, like this: 另一个解决方案是在函数内部使用前向声明,如下所示:

void Bar::foo()
{
    void foo(int);
    foo(1);
}

As Praetorian suggests, here is another option: 正如Praetorian所建议的,这是另一种选择:

void Bar::foo()
{
    using ::foo;
    foo(1);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么类不能为函数和数据成员具有相同的名称? - Why can't a class have same name for a function and a data member? 为什么我们可以在定义之前调用 class 方法,但不能对自由函数做同样的事情? - Why can we call a class method before it's defined, but can't do the same with free functions? 调用与方法同名的函数 - Call function with same name as method 使用具有相同声明的类方法调用全局函数 - calling a global function with a class method with the same declaration 为什么内部 class 和外部 class 不能同名? - Why can't inner class and outer class have same name? 为什么 function 名称不能与返回名称类型相同? - Why can't function name be the same as return name type? 为什么不能使用与成员类型同名的方法? - Why can't I have a method with the same name as a member type? 从具有相同名称的未命名命名空间中的函数调用全局函数 - Global function call from function in unnamed namespace with the same name 可以从具有相同名称的派生 class ZC1C425268E68385D14AB5074C17Z9 中调用基础 class function。 但我找不到支持这一点的标准报价 - One can call a base class function from a derived class function with the same name. But I can't find a quote from the Standard supporting this class static 成员 function 在全局 ZC1C425268E68385D1AB5074C17A4 中选择同名 - class static member function chosen over global function with same name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM