【发布时间】:2014-05-08 11:42:39
【问题描述】:
我有以下代码:
struct M {
friend void f() {}
M() {
f(); // error: 'f' was not declared in this scope
}
};
int main() {
M m;
}
g++4.8 和 clang3.4 都无法编译它,因为 f 在 M 中不可见,或者他们是这么说的。
但是,标准给出了类似代码的示例
class M {
friend void f() { } // definition of global f, a friend of M,
// not the definition of a member function
};
然后说
类中定义的
friend函数位于 定义它的类。
(ISO/IEC 14882:2011 11.3 Friends [class.friend] p6, p7)
从这里我无法理解编译器如何找不到在使用它的同一类中定义的f。
两个编译器不太可能有相同的错误。
那么,我错过了什么?
【问题讨论】:
-
你在哪里声明的?
标签: c++