成员不可访问.过载+操作员
Member is inaccessible. Overloading + Operator
这就是我要做的。我要把f1和f2的每个成员加在一起_m是斜率,b是y深度。
linear_function operator +
(const linear_function& f1, const linear_function& f2)
{
linear_function f;
f._m = f1._m + f2._m;
f._b = f1._b + f2._b;
return f;
}
但它表示,无法访问这些成员。这是.h文件。
#ifndef LINEAR_H
#define LINEAR_H
#include <iostream> // Provides ostream
namespace main_savitch_2
{
class linear_function
{
public:
// CONSTRUCTOR
linear_function(double _b = 0.0, double _m = 0.0);
// MODIFICATION MEMBER FUNCTIONS
void set(double _b, double _m);
// CONSTANT MEMBER FUNCTIONS
double eval(double x) const;
double root() const;
double slope() const;
double y_intersept() const;
// CONSTANT OPERATORS
double operator ( ) (double x) const;
private:
double _m, _b;
};
// NON-MEMBER BINARY OPERATORS
linear_function operator +
(const linear_function& f1, const linear_function& f2);
linear_function operator -
(const linear_function& f1, const linear_function& f2);
linear_function operator |
(const linear_function& f1, const linear_function& f2);
// NON-MEMBER OUTPUT FUNCTIONS
std::ostream& operator << (std::ostream& out, const linear_function& p);
}
#endif
我在谷歌上搜索了成员与非成员运算符重载,并使我的运算符与显示的内容相匹配。不确定我为什么会出现这个错误。
由于成员_m
和_b
是私有的,因此只有类的成员及其友元函数才能访问它们。为了允许外部函数访问成员,您需要将friend
关键字添加到类内运算符的声明中,如下所示:
friend linear_function operator |
(const linear_function& f1, const linear_function& f2);
friend linear_function operator -
(const linear_function& f1, const linear_function& f2);
friend linear_function operator |
(const linear_function& f1, const linear_function& f2);
也许friend
是你的朋友。尝试在类中将运算符声明为friend
函数,以便提供对类private
成员的访问权限。
class linear_function
{
....
friend linear_function operator +
(const linear_function& f1, const linear_function& f2);
friend linear_function operator -
(const linear_function& f1, const linear_function& f2);
friend linear_function operator |
(const linear_function& f1, const linear_function& f2);
}
比使用friend
更好的选择(尤其是如果您无论如何都想要这些运算符)是使用成员分配运算符:
class linear_function
{
// ...
linear_function & operator+= (linear_function const &f)
{
_m += f._m;
_b += f._b;
return *this;
}
};
// non-member but does not need special access rules now
linear_function operator+ (linear_function f1, linear_function const &f2)
{
return f1 += f2;
}
更新:包含成员operator+=
的示例实现。通常情况下,我会将其声明为内联,并将定义放在.cpp
文件中,但为了使示例简单起见,我在此处显示了内联定义。
考虑以下内容:
class linear_function
{
public:
...
linear_function operator+(const linear_function &other) const
{
return linear_function(_b + other._b, _m + other._m);
}
};
如果您无法修改此类的定义,只需保留现有的类外定义,并使用getter linear_function::slope()
和linear_function::y_intercept()
,它们获取_m
和_b
的值:
linear_function operator+(const linear_function &l, const linear_function &r)
{
return linear_function(l.y_intercept() + r.y_intercept(),
l.slope() + r.slope());
}
相关文章:
- C++中>>操作员过载时出现问题?
- SFINAE不能防止模棱两可的操作员过载吗?
- 在这种情况下是私有的吗?试图使操作员<<过载
- C++操作员过载>>
- 需要帮助操作员过载,这样我才能显示功能
- C++操作员过载?
- 如何在结构内使操作员<<过载
- C++ - 试图使操作员"<<"过载
- 何时必须使操作员<<过载?
- C 如何在另一类中使用操作员过载
- 操作员过载但不可行
- 不能<<操作员过载
- C /CLI操作员=过载参考
- 提升::访客操作员过载的变体"Error: no match for call to [...]"
- 操作员+过载有什么问题?
- 是否可以在C++中使操作员"..."过载?
- 旋转测试操作员过载
- 操作员过载功能
- C++ 操作员过载问题
- 操作员过载的正确方法是什么,为什么这不起作用