search

Home  >  Q&A  >  body text

c++ - 为什么不能在局部类中定义友元函数,而全局类就可以?

1

2

3

4

5

6

7

8

9

10

11

12

13

<code>#include <iostream>

#include <vector>

#include <string>

using namespace std;

//struct X {

//    friend void f() {}//这里就没问题

//};

int main()

{

    struct X {

        friend void f() {}//error C2689: “f”: 不能在局部类中定义友元函数

    };

}</code>

阿神阿神2808 days ago579

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 12:07:57

    C and C++ functions cannot define functions inside functions.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 12:07:57

    The scope of local classes is already very limited. Do you think it is necessary to declare friends? Why is there such a need?

    reply
    0
  • Cancelreply