友元函数无法访问私有类成员
friend function has no access to private class members
我仍然是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);
实际上,您可能更喜欢编辑现有的朋友,它看起来很像拼写错误或其他类型的琐碎错误。
相关文章:
- 这是关于成员访问规则的正确摘要吗
- 为什么我在空指针错误(链表)中获取成员访问权限
- 成员访问是否在空指针上定义C++?
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问
- 为什么类成员数据必须是静态的才能被模板化类的模板化结构成员访问
- 为什么c++允许成员函数定义中实例的私有成员访问
- C/C++ 包含点的宏参数(成员访问运算符)
- 访问说明符(私有/公共/受保护)如何在内部工作(限制成员访问)?
- 如何将超类的受保护成员访问到其派生类. 如果已在派生类中声明了具有相同名称的函数?
- 内部类私有成员访问和封闭的友好性
- 通过 C++ 中的另一个结构成员访问结构
- 具体化 PRVALUES 成员访问的 decltype 行为不正确
- 常量表达式中的静态成员访问
- XVALUE来自类成员访问表达式
- 未经授权的私有类成员访问会产生编译时错误而不是运行时错误?
- 在 c++ 中,为什么 -> 被称为二进制中缀指针成员访问运算符?
- 如何访问模板参数的成员?“成员访问不完整的类型”
- 不明确的可变参数类成员访问
- C 受保护的成员访问
- 将typeID转换为静态成员访问(C )的命名空间