成员不可访问.过载+操作员

Member is inaccessible. Overloading + Operator

本文关键字:操作员 过载 访问 成员      更新时间:2023-10-16

这就是我要做的。我要把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());
}