【发布时间】:2010-11-06 17:05:43
【问题描述】:
按照标准:
在类中定义的友元函数在定义它的类的(词法)范围内。
那为什么这不起作用(测试了多个 GCC 版本)?
#include <iostream>
using namespace std;
class A
{
friend void function() { cout << "text" << endl; };
};
// void function();
int main()
{
function();
return 0;
}
取消注释当然可以解决问题。
编辑(gcc 输出):
(xterm) $ g++ -ansi -pedantic -Wall -Wextra test.cpp
test.cpp: In function ‘int main()’:
test.cpp:13:11: error: ‘function’ was not declared in this scope
【问题讨论】:
-
你能发布你得到的错误吗?