C ++向前声明,朋友函数问题 [英] C++ Forward declaration , friend function problem

查看:143
本文介绍了C ++向前声明,朋友函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>

class B;

class A{
 int a;
public:
 friend void B::frndA();
};

class B{
 int b;
public:
 void frndA();
};

void B::frndA(){
 A obj;
 std::cout << "A.a = " << obj.a << std::endl;
}

int main() {
 return 0;
}



当我试图编译此代码时发生了一些错误。任何人都可以解释此代码中的问题。

When I am trying to compile this code some error occurred. Can anyone explain what are the problems in this code

推荐答案

将整个 A类之前的声明。您尚未声明 B :: frndA();

#include <iostream>
using namespace std;

class B{
    int b;
public:
    void frndA();
};

class A{
    int a;
public:
    friend void B::frndA();
};



void B::frndA(){
    A obj;
    //cout<<"A.a = "<<obj.a<<endl;
}

int main() {
    return 0;
}

这篇关于C ++向前声明,朋友函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆