编译以下内容:

void bar() { /* ... */ }

void foo()
{
    struct MyStruct
    {
        friend void bar();
    };
}

int main()
{
    //..
}

导致错误:



为什么名称查找失败?我该如何解决?

最佳答案

即使您因为The name of a local class is local to its enclosing scope-§9.8/ 1的缘故而对本地类进行访问,也不能超出其封闭范围。

但是,如果您只想让它编译,请明确告诉它在全局范围内显示...

friend void ::bar();

*由于某些原因,这可以在VS中修复,但不能在GCC中修复

§11.3/ 11(感谢jrok)

关于c++ - 在本地类中查找 friend 功能的名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8718643/

10-11 22:42
查看更多