小编lin*_*ack的帖子

如何只为一个特定的函数和类声明友元函数?

我的代码出了什么问题?

我试图在GNU G ++环境中编译下面的代码,我得到这些错误:

friend2.cpp:30: error: invalid use of incomplete type ‘struct two’
friend2.cpp:5: error: forward declaration of ‘struct two’
friend2.cpp: In member function ‘int two::accessboth(one)’:
friend2.cpp:24: error: ‘int one::data1’ is private
friend2.cpp:55: error: within this context
#include <iostream>
using namespace std;

class two;

class one
{
    private:
        int data1;
    public:
        one()
        {
            data1 = 100;
        }

        friend int two::accessboth(one a);
};

class two
{
    private:
        int data2;

    public:
        two()
        {
            data2 = 200;
        }

        int accessboth(one a);
};

int …
Run Code Online (Sandbox Code Playgroud)

c++ friend-function

1
推荐指数
1
解决办法
806
查看次数

标签 统计

c++ ×1

friend-function ×1