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);
}
Well, the reason for the no match for 'operator>>'
error is
that there isn't any match for the operator>>
, at least not in
the code you've shown. The only operator>>
and operator<<
in the code you've shown are for Packet::CommonHeader
and for
Packet
. Nothing for a quint32
, nor for a QTime
, nor for
a PacketType
, nor for an int
.
For that matter, the implementations you've shown us are for
Packet::CommonHeader
and Packet
; the classes, however, are
in 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).