访问类中定义的友元函数

发布于 2024-12-10 01:34:09 字数 556 浏览 0 评论 0原文

有这样的代码:

#include <iostream>

class A{

public:
    friend void fun(A a){std::cout << "Im here" << std::endl;}
    friend void fun2(){ std::cout << "Im here2" << std::endl; }
    friend void fun3();
};

void fun3(){
    std::cout << "Im here3" << std::endl;
}

int main() 
{  
    fun(A()); // works ok
    //fun2(); error: 'fun2' was not declared in this scope
    //A::fun2(); error: 'fun2' is not a member of 'A'
    fun3(); // works ok
} 

How to access function fun2()?

There is such code:

#include <iostream>

class A{

public:
    friend void fun(A a){std::cout << "Im here" << std::endl;}
    friend void fun2(){ std::cout << "Im here2" << std::endl; }
    friend void fun3();
};

void fun3(){
    std::cout << "Im here3" << std::endl;
}

int main() 
{  
    fun(A()); // works ok
    //fun2(); error: 'fun2' was not declared in this scope
    //A::fun2(); error: 'fun2' is not a member of 'A'
    fun3(); // works ok
} 

How to access function fun2()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘心安 2024-12-17 01:34:09
class A{

public:
    friend void fun(A a){std::cout << "Im here" << std::endl;}
    friend void fun2(){ std::cout << "Im here2" << std::endl; }
    friend void fun3();
};

尽管您对 fun2 的定义确实定义了一个“全局”函数而不是成员,并使其成为 Afriend code> 同时,您仍然缺少全局范围本身中同一函数的声明。

这意味着该范围内的任何代码都不知道 fun2 存在。

fun 也会出现同样的问题,只不过参数相关查找可以接管并查找该函数,因为有一个 A 类型的参数。

我建议您以通常的方式定义您的函数:

class A {
   friend void fun(A a);
   friend void fun2();
   friend void fun3();
};

void fun(A a) { std::cout << "I'm here"  << std::endl; }
void fun2()   { std::cout << "I'm here2" << std::endl; }
void fun3();

现在请注意,一切都可以工作(除了fun3 因为我从未定义过它)。

class A{

public:
    friend void fun(A a){std::cout << "Im here" << std::endl;}
    friend void fun2(){ std::cout << "Im here2" << std::endl; }
    friend void fun3();
};

Although your definition of fun2 does define a "global" function rather than a member, and makes it a friend of A at the same time, you are still missing a declaration of the same function in the global scope itself.

That means that no code in that scope has any idea that fun2 exists.

The same problem occurs for fun, except that Argument-Dependent Lookup can take over and find the function, because there is an argument of type A.

I recommend instead defining your functions in the usual manner:

class A {
   friend void fun(A a);
   friend void fun2();
   friend void fun3();
};

void fun(A a) { std::cout << "I'm here"  << std::endl; }
void fun2()   { std::cout << "I'm here2" << std::endl; }
void fun3();

Notice now that everything works (except fun3 because I never defined it).

美胚控场 2024-12-17 01:34:09

您可以调用 fun 的原因是类 A 内的友元声明使其仅通过参数相关查找可见。否则,友元声明不会使它们声明的函数在出现的类范围之外自动可见。

您需要在命名空间范围或 main 内部添加声明,以使 fun2main 中可见。

例如,

void fun2();

fun3main 内部可见,因为它的定义(在类外部)也是一个声明,使其在 main 中可见。

ISO/IEC 14882:2011 7.3.1.2:

在该命名空间范围内(在授予友谊的类定义之前或之后)提供匹配声明之前,通过非限定查找 (3.4.1) 或限定查找 (3.4.3) 无法找到好友的名称。

3.4.2(依赖于参数的名称查找)/4:

关联类中声明的任何名称空间范围的友元函数或友元函数模板在其各自的名称空间内都是可见的,即使它们在普通查找期间不可见 (11.3)。

The reason that you can call fun is that the friend declaration inside class A makes it visible via argument dependent lookup only. Otherwise friend declarations don't make the functions that they declare automatically visible outside of the class scope where the appear.

You need to add a declaration at namespace scope or inside main to make fun2 visible in main.

E.g.

void fun2();

fun3 is visible inside main because its definition (outside of the class) is also a declaration that makes it visible from main.

ISO/IEC 14882:2011 7.3.1.2:

The name of the friend is not found by unqualified lookup (3.4.1) or by qualified lookup (3.4.3) until a matching declaration is provided in that namespace scope (either before or after the class definition granting friendship).

3.4.2 (Argument-dependent name lookup) / 4:

Any namespace-scope friend functions or friend function templates declared in associated classes are visible within their respective namespaces even if they are not visible during an ordinary lookup (11.3).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文