简体   繁体   English

朋友功能无法访问私人班级成员

[英]friend function has no access to private class members

I am still new to C++ and as part of an assignment I have written a class that needs an overload of the stream extraction operator '>>' for file stream extraction in order to make things a bit easier, so says the instructions. 我对C ++还是陌生的,作为作业的一部分,我编写了一个类,该类需要重载流提取运算符'>>'来进行文件流提取,以使事情变得容易一些,因此,这些说明说。 I have declared and defined 2 overloads for both operators, one oveload for iostream objects and one overload for fstream object. 我已经为这两个运算符声明并定义了2个重载,一个iostream对象的oveload和一个fstream对象的重载。 Now, everything is fine until i get to the definition of '>>' for file stream objects, apparently that function has no access to the private (or protected) member of the class of which it is a friend of. 现在,一切都很好,直到我为文件流对象定义了“ >>”为止,显然,该函数无法访问与其成为朋友的类的私有(或受保护)成员。

Here is my code, i thank you all in advance: 这是我的代码,在此先感谢大家:

stock.h 股票.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, i will only include the function for which an error is generated 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;
}

the error is that for all the attributes that are listed are "inaccessible" 错误是,对于列出的所有属性都是“不可访问的”

I'd like to finish this assignment as I'd like to move on to another one that is due on exception handling. 我想完成此任务,因为我想转到由于异常处理而引起的另一任务。

Thank you all in advance, once again. 再次谢谢大家。

i apologize, I believe that the signatures in the definition and implementation were different, as pointed out by a previous comment, i declared a friend function twice and there i saw my error, i did not change the code after copy/pasting it. 抱歉,我认为定义和实现中的签名不同,正如先前的评论所指出的那样,我两次声明了一个朋友函数,并且在那里看到了我的错误,在复制/粘贴后没有更改代码。 For all future readers: MAKE SURE DECALRATION SIGNATURES MATCH IMPLEMENTATION SIGNATURES! 对于所有将来的读者:确保使用除垢签名,然后匹配实施签名!

There is a 有一个

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

but you have access problems in 但您在

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

Adding another friend should help. 添加另一个朋友应该会有所帮助。

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

Actually, you might prefer to edit the existing friend, it very much looks like a typo or other kind of trivial error. 实际上,您可能更喜欢编辑现有的朋友,这看起来像是一个错字或其他类型的琐碎错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cpp Friend功能无法访问私有静态成员 - Cpp Friend function has no access to private static members 为什么好友功能无法访问班级的私人成员 - Why friend function is not able to access private members of the class 如何在header class中访问好友function中的私人会员 - How to access private members in friend function in header class 类具有朋友功能。 该函数在另一个头文件中的名称空间内定义。 它不能访问私人成员? - Class has a friend function. The function is defined inside of a namesapce in a different header file. It cannot access private members? 无法访问朋友班的私人成员 - Cannot access friend class's private members 有班级的朋友但无法访问私人会员 - friend with class but can't access private members 朋友功能无法访问私人会员 - Friend function can't access private members 如何启用朋友班的朋友 function 直接在 C++ 中访问其私有成员 - How to enable a friend class's friend function access its private members directly in C++ 朋友功能无法访问另一个朋友类的私人成员 - Friend function is not accessing private members of another friend class 在B类中声明为朋友的A类成员模板函数无法访问A类的私有成员(仅限Clang) - Class A member template function declared as friend in class B can't access private members of class A (Clang only)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM