0

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);
}
Neel Basu
  • 12,638
  • 12
  • 82
  • 146

2 Answers2

1

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).

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • Doing `DG::operator<<` in cpp solved the friend Problem. But its `DG::operator<<` not Global `operator<<` So would it work for operator overloading ? – Neel Basu Apr 18 '11 at 17:19
  • The compiler will find it anytime either of the arguments somehow implicate `DG`; e.g. if `QDataStream` is in `DG`, always. Otherwise, you can make `::operator>>` etc. `friend`, or you can provide a public member function `print` or whatever, and call that from `operator<<`, thus avoiding the need of `friend`. – James Kanze Apr 18 '11 at 18:03
  • But `QDataStream` is not in `DG` – Neel Basu Apr 18 '11 at 18:33
  • In which case, you probably don't want the `operator<<` et al. in `DG` either, and should make `::operator<<` and `::operator>>` friend (or provide public `print` and `scan` functions which they call). – James Kanze Apr 19 '11 at 08:28
0

First remove the const from the rhs parameters of in the >> operators as you are modifying them.

snoofkin
  • 8,725
  • 14
  • 49
  • 86