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

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 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?