[英]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.