【问题标题】:friend function has no access to private class members朋友功能无法访问私有类成员
【发布时间】:2018-04-15 16:35:25
【问题描述】:

我还是 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;
}

错误是列出的所有属性都是“不可访问”

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

再次感谢大家。

【问题讨论】:

  • 您不需要文件流的重载。 std::ifstream 继承自 std::istream。与输出流相同。
  • 那为什么非文件流操作符的友元函数要声明两次呢?
  • 您是否打算将std::ifstream&amp; operator&gt;&gt;(std::ifstream&amp; if_obj 列入好友名单?
  • @Someprogrammerdude 所以,假设我有一种方法可以将类对象的内容写入文本文件,你是说我为 iostream 所做的重载足以执行此操作吗?如果是这样,那就太好了!
  • 一个输入流是一个输入流是一个输入流。不管是std::cinstd::ifstream 对象还是std::istringstream 对象。与输出变量相同。

标签: c++ operator-overloading


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    有一个

    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);
    

    实际上,您可能更喜欢编辑现有的朋友,它看起来很像一个错字或其他类型的小错误。

    【讨论】:

      猜你喜欢
      • 2015-11-24
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      相关资源
      最近更新 更多