Friend Feature Availability

class C2;   //Forward Declaration

class C1
{
    int status;

    public:
    void set_status(int state);
    void get_status(C2 y);
};

class C2
{
    int status;

    public:
    void set_status(int state);
    friend void C1::get_status(C2 y);
};

//Function Definitions

void C1::set_status(int state)
{
    status = state;
}

void C2::set_status(int state)
{
    status = state;
}

void C1::get_status(C2 y)   //Member function of C1
{
    if (y.status | status)
    cout<<" PRINT " <<endl;
}

y.status the second last line displays an error:

C2 :: status not available

The code runs correctly, but there is a red line (error) under y.status.

Why is this?

+4
source share
1 answer

It seems to me that the compiler (or part of the compiler) used by your IDE has a problem. I have added enough of your code to get the full program:

#include <iostream>
using namespace std;


class C2;   //Forward Declaration

class C1
{
    int status;

public:
    void set_status(int state);
    void get_status(C2 y);
};

class C2
{
    int status;

public:
    void set_status(int state);
    friend void C1::get_status(C2);
};

//Function Definitions

void C1::set_status(int state) {
    status = state;
}

void C2::set_status(int state) {
    status = state;
}

void C1::get_status(C2 y)   //Member function of C1
{
    if (y.status | status)
        cout << " PRINT " << endl;
}

int main() {

    C1 c;
    C2 d;
    d.set_status(1);

    c.get_status(d);
}

Compiled (without errors or warnings, with default flags) with g ++ 4.9 and VC ++ 12 (also known as VC ++ 2013). Both produced: PRINTas a way out.

IDE , , , , .

+6

Source: https://habr.com/ru/post/1589075/


All Articles