友元函数无法访问私有类成员

friend function has no access to private class members

本文关键字:成员 访问 函数 友元      更新时间:2023-10-16

我仍然是C++新手,作为作业的一部分,我编写了一个类,该类需要重载流提取运算符">>"用于文件流提取,以使事情变得更容易一些,说明如此说。我已经为两个运算符声明并定义了 2 个重载,一个用于 iostream 对象,一个重载用于 fstream 对象。现在,一切都很好,直到我得到文件流对象的">>"的定义,显然该函数无法访问它是其朋友的类的私有(或受保护(成员。

这是我的代码,我提前感谢大家:

股票.h

#ifndef STOCK_H
#define STOCK_H
#include<iostream>
#include<fstream>

class Stock_Type 
{
    friend std::ostream& operator<<(std::ostream&, const Stock_Type&);
    friend std::istream& operator>>(std::istream&, Stock_Type&);
    friend std::ofstream& operator<<(std::ofstream&, const Stock_Type&);
    friend std::ifstream& operator>>(std::ofstream&, Stock_Type&);
    public:
        //constructor overloads-----------------------------------------------------------------------------------------------------
        Stock_Type(){};
        Stock_Type(std::string sym, double a, double b, double c, double d, double e, int f, double g) :
            stock_symbol(sym), opening_price(a), closing_price(b), high_price(c), low_price(d), prev_close(e), volume(f), percent_gain(g) {}
        //default destructor--------------------------------------------------------------------------------------------------------
        ~Stock_Type(){};
        //accessor functions--------------------------------------------------------------------------------------------------------
        void set_Symbol(std::string x){stock_symbol = x;}
        void set_Closing_Price(double x){closing_price = x;}
        void set_High_Price(double x){high_price = x;}
        void set_Low_Price(double x){low_price = x;}
        void set_Prev_Close(double x){prev_close = x;}
        void set_Volume(int x){volume = x;}
        std::string get_Stock_Smybol(){return stock_symbol;}
        double get_Opening_Price(){return opening_price;}
        double get_Closing_Price(){return closing_price;}
        double get_High_Price(){return high_price;}
        double get_Low_Price(){return low_price;}
        double get_Prev_Close(){return prev_close;}
        int get_Volume(){return volume;}
        double get_Percent_Gain_Loss(){return get_Closing_Price() - get_Opening_Price();}
        //operations on Stock_Type-------------------------------------------------------------------------------------------------------

        //operator functions--------------------------------------------------------------------------------------------------------------
        bool operator==(const Stock_Type&)const;
        bool operator!=(const Stock_Type&)const;
        bool operator<(const Stock_Type&) const;
        bool operator<=(const Stock_Type&)const;
        bool operator>(const Stock_Type&)const;
        bool operator>=(const Stock_Type&)const;
        friend std::ostream& operator<<(std::ostream&, const Stock_Type&);
        friend std::istream& operator>>(std::istream&, Stock_Type&);
        const Stock_Type& operator=(const Stock_Type &right_operand);
        Stock_Type& operator[](int elem);
        const Stock_Type& operator[](int elem) const;

    private:
        std::string stock_symbol;//record data1
        double opening_price, closing_price, high_price, low_price, prev_close;//record data2
        int volume;//record data3
        double percent_gain;//record data 4
        Stock_Type *stock_pointer;
        int array_size;

};


#endif

stock.cpp,我将只包含为其生成错误的函数

std::ifstream& operator>>(std::ifstream& if_obj, Stock_Type& stock_obj)
{
    if_obj >> stock_obj.stock_symbol 
          >> stock_obj.opening_price
          >> stock_obj.closing_price 
          >> stock_obj.high_price
          >> stock_obj.low_price
          >> stock_obj.prev_close
          >> stock_obj.volume
          >> stock_obj.percent_gain;
    return if_obj;
}

错误是列出的所有属性都是"无法访问">

我想

完成这项任务,因为我想继续进行另一项异常处理。

再次提前感谢大家。

很抱歉,我相信定义和实现中的签名是不同的,正如之前的评论所指出的,我声明了一个朋友函数两次,在那里我看到了我的错误,复制/粘贴后我没有更改代码。对于所有未来的读者: 确保降级签名与实现签名匹配!

有一个

friend std::ifstream& operator>>(std::ofstream&, Stock_Type&);

但是您在

std::ifstream& operator>>(std::ifstream& if_obj, Stock_Type& stock_obj)

添加另一个朋友应该会有所帮助。

friend std::ifstream& operator>>(std::ifstream& if_obj, Stock_Type& stock_obj);

实际上,您可能更喜欢编辑现有的朋友,它看起来很像拼写错误或其他类型的琐碎错误。