【发布时间】:2015-12-10 16:52:30
【问题描述】:
我正在学习友元函数 (C++),但我不明白为什么这段代码不起作用。我明白了
错误:“错误 C2027:使用未定义的类型‘第二’”。 (第 6 行)
这当然只是一个例子(没用)。我正在尝试使用另一个类的成员函数作为朋友(只是那个函数)。我在网上找到了一些例子。但是在这里的一篇旧帖子中,有人说另一个类的成员函数不能成为一个类的朋友。这是真的吗?
#include<iostream>
using namespace std;
class second;
class test
{
friend void second::fun(test &);
public:
int j = 89;
private:
int t = 12;
};
class second
{
public:
void fun(test &b)
{
cout << "Try " << b.j << endl;
}
int a = 29;
private:
int u = 10;
};
int main()
{
test b;
second one;
one.fun(b);
return 0;
}
【问题讨论】:
标签: c++ class friend-function