简体   繁体   English

即使该类已声明为友元类,数据成员也无法访问

[英]data member is inaccessible even though the class has been declared as friend class

The data member is inaccessible even though the class has been declared as friend class.即使该类已声明为友元类,数据成员也无法访问。 error: 'total' was not declared in this scope错误:'total' 未在此范围内声明

#include <iostream>
#include <iomanip>
using namespace std;

class Produce
{
    private:
        float total;
    public:
        Produce()
        {
            total=0;
        }
        friend class Bill;
};
class Bill
{
    private:
        float grand_total;
    public:
    Bill()
    {
        grand_total=0;
    }
    Bill operator+=(const Produce &p)
    {
        return Bill(total+p.total);
    }
};

The error message is correct: there is no name total in the current scope.错误信息是正确的:当前作用域中没有名称total The code should either refer to grand_total , which is a member of the class Bill , which in turn defines the operator+= that's being written, or it should refer to p.total to access the total that's a member of the class Produce .代码应该要么引用grand_total ,它是类Bill的成员,后者又定义了正在编写的operator+= ,或者它应该引用p.total来访问属于类Produce成员的total

Making class A a friend of class B doesn't mean that you can just randomly name non-static members of B from within A , without naming an instance of B to use.使A类成为B类的朋友并不意味着您可以从A随机命名B非静态成员,而无需命名B的实例以供使用。

Which Produce::total (out of potentially infinite) do you want your program to use?您希望您的程序使用哪个Produce::total (可能是无限的)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 会员无法通过朋友班访问 - Member is inaccessible from friend class 无法访问在类中声明的私人成员,甚至无法访问声明为朋友的类 - Cannot access private member declared in class, even declared friend class 为什么即使我已经声明了 friend 类,我仍会收到错误消息“无法访问在类中声明的私有成员” - Why am I getting the error 'cannot access private member declared in class' even though I have declared friend class 类数据成员无法访问 - Class Data Member Inaccessible (朋友类的)成员“Node::next”不可访问 - member "Node::next" (of a friend class) is inaccessible C++:成员不可访问 - 使用朋友 function 允许一个 class 修改另一个 ZA2F2ED4F8EBC2CBB4ZC2A 成员数据 - C++: Member is inaccessible - using friend function to allow one class to modify another class member data 操作符重载(使用binaray好友函数)类没有成员,并且成员不可访问 - operator overloading(using binaray friend function) class has no member, and member inaccessible “类”尚未声明 - “Class” has not been declared C ++ istream运算符重载-即使声明为好友也无法访问数据成员 - C++ istream operator overload - cant access data member even though declared friend Class 没有朋友的成员方法 - Class has no member of a friend method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM