朋友没有获得私人会员
我有一个名为 Packet
的类,我想用 QDataStream
序列化它,我重载了 operator>>
和 operator<<< /code> 并在重载函数中我调用
stream << somIntMember 虽然它声明为朋友,但它抱怨私有变量,
error: 'int DG::Packet::_state' is private
error: 'DG::Packet::PacketType DG::Packet::_type' is private
这是我的标题。
namespace DG{
class Packet{
public:
struct CommonHeader{
public:
quint32 id;
QTime time;
quint32 size;
PacketType packetType;
public:
CommonHeader();
CommonHeader(quint32 sz, PacketType type);
friend QDataStream& operator<<(QDataStream&, const Packet::CommonHeader& header);
friend QDataStream& operator>>(QDataStream&, Packet::CommonHeader& header);
};
private:
PacketType _type;
int _state;
public:
friend QDataStream& operator<<(QDataStream&, const Packet& packet);
friend QDataStream& operator>>(QDataStream&, Packet& packet);
};
}
西奥德来了
#include "packet.h"
using namespace DG;
QDataStream& operator<<(QDataStream& stream, const Packet& packet){
stream << packet._state << packet._type;
return packet.serialize(stream);
}
I've a class called Packet
That I want to serialize with QDataStream
I overloaded operator>>
and operator<<
and in the over loaded function I called stream << somIntMember
Though Its declared as friend its complaining for Private Variables
error: 'int DG::Packet::_state' is private
error: 'DG::Packet::PacketType DG::Packet::_type' is private
Here goes my Header.
namespace DG{
class Packet{
public:
struct CommonHeader{
public:
quint32 id;
QTime time;
quint32 size;
PacketType packetType;
public:
CommonHeader();
CommonHeader(quint32 sz, PacketType type);
friend QDataStream& operator<<(QDataStream&, const Packet::CommonHeader& header);
friend QDataStream& operator>>(QDataStream&, Packet::CommonHeader& header);
};
private:
PacketType _type;
int _state;
public:
friend QDataStream& operator<<(QDataStream&, const Packet& packet);
friend QDataStream& operator>>(QDataStream&, Packet& packet);
};
}
And here goes the Ciode
#include "packet.h"
using namespace DG;
QDataStream& operator<<(QDataStream& stream, const Packet& packet){
stream << packet._state << packet._type;
return packet.serialize(stream);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,
no match for 'operator>>'
错误的原因是operator>>
没有任何匹配项,至少在您所显示的代码。唯一的
operator>>
和operator<<
在您显示的代码中,用于
Packet::CommonHeader
和数据包
。对于quint32
、对于QTime
以及对于PacketType
,也不是int
。就此而言,您向我们展示的实现是为了
Packet::CommonHeader
和Packet
;然而,这些课程是在命名空间
DG
中,而不是在全局命名空间中。这也可以解释
friend
不起作用的原因。您声明为友元的运算符位于命名空间
DG
中,您定义的那些位于全局命名空间中(因此
完全不相关的功能)。
Well, the reason for the
no match for 'operator>>'
error isthat there isn't any match for the
operator>>
, at least not inthe code you've shown. The only
operator>>
andoperator<<
in the code you've shown are for
Packet::CommonHeader
and forPacket
. Nothing for aquint32
, nor for aQTime
, nor fora
PacketType
, nor for anint
.For that matter, the implementations you've shown us are for
Packet::CommonHeader
andPacket
; the classes, however, arein namespace
DG
, and not in the global namespace.That could also explain the reason why
friend
isn't working.The operators you've declared as friend are in namespace
DG
,those you define are in the global namespace (and are thus
completely unrelated functions).
首先将>>中的rhs参数中的const去掉修改操作符时。
First remove the const from the rhs parameters of in the >> operators as you are modifying them.