Kong ling 2017-10-01 04:53 采纳率: 0%
浏览 1991

c++友元函数重载<<运算符后还是无法访问类私有变量。求大神解答

#ifndef A_H_
#define A_H_
class Stonewt
{
private:
enum { Lbs_per_stn = 14 };
enum Mode { Stone, IntPounds, FloatPounds };
Mode m_mode;
int mm_stone;
double m_left_pbs;
double m_pounds;
public:
Stonewt();
Stonewt(double weight, Mode mode);
~Stonewt();
friend Stonewt operator +(Stonewt& temp1, Stonewt& temp2);
friend ostream& operator <<(ostream& os, Stonewt& temp);
};
#endif
//以上为头文件

#include
#include"标头.h"
using namespace std;
Stonewt::Stonewt()
{
mm_stone = 0;
m_left_pbs = 0;
m_pounds = 0;
}
Stonewt::Stonewt(double weight, Mode mode)
{
m_mode = mode;
if (m_mode == Stone)
{
mm_stone = weight;
m_left_pbs = 0;
m_pounds = weight*Lbs_per_stn;
}
else if (m_mode == IntPounds)
{
mm_stone = (int)weight / Lbs_per_stn;
m_left_pbs = (int)weight%Lbs_per_stn;
m_pounds = (int)weight*Lbs_per_stn;
}
else if (m_mode == FloatPounds)
{
mm_stone = weight / Lbs_per_stn;
m_left_pbs = (int)weight%Lbs_per_stn + weight - int(weight);
m_pounds = weight*Lbs_per_stn;
}
else
{
cout << "模式错误\n";
mm_stone = 0;
m_left_pbs = 0;
m_pounds = 0;
}
}
Stonewt::~Stonewt()
{
}
Stonewt operator +(Stonewt& temp1, Stonewt& temp2)
{
Stonewt temp;
temp.mm_stone = temp1.mm_stone + temp2.mm_stone;
return temp;

}
ostream& operator<<(ostream& os, Stonewt& temp)
{
os << "英石:" << temp.mm_stone << " 磅:" << temp.m_left_pbs << endl
<< "总英石:" << temp.m_pounds << endl;
return os;
}
//以上为类实现文件
在最后一个operator函数中无法访问 temp.mm_stone;temp.m_left_pbs; temp.m_pounds 。但是把类的定义和类方法写在同一个文件上却不抱错不知道为什么,我用的VA2015图片说明

  • 写回答

5条回答 默认 最新

  • worm0336 2017-10-01 15:04
    关注

    呵呵,在实现文件中,估计没有包含类的头文件

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题