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;
}
Run Code Online (Sandbox Code Playgroud)
y.status 在倒数第二行显示错误:
C2 ::状态无法访问
代码执行正常,但下面有一个红线(错误)y.status.
为什么是这样?
听起来像IDE使用的编译器(或编译器的一部分)有问题.我添加了足够的代码来获得完整的程序:
#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);
}
Run Code Online (Sandbox Code Playgroud)
使用g ++ 4.9和VC++ 12(又名VC++ 2013)编译(没有错误或警告,带有默认标志).两者都产生:PRINT作为输出.
IDE将源代码与实际编译器分开解析是很常见的,而且与真正的编译器相比,其中一些相当有限,所以我猜他们对某些事情感到困惑并不奇怪.