朋友没有获得私人会员

发布于 2024-11-02 13:33:09 字数 1405 浏览 4 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云醉月微眠 2024-11-09 13:33:09

那么,no match for 'operator>>' 错误的原因是
operator>> 没有任何匹配项,至少在
您所显示的代码。唯一的operator>>operator<<
在您显示的代码中,用于 Packet::CommonHeader
数据包。对于 quint32、对于 QTime 以及对于
PacketType,也不是 int

就此而言,您向我们展示的实现是为了
Packet::CommonHeaderPacket;然而,这些课程是
在命名空间 DG 中,而不是在全局命名空间中。

这也可以解释 friend 不起作用的原因。
您声明为友元的运算符位于命名空间 DG 中,
您定义的那些位于全局命名空间中(因此
完全不相关的功能)。

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

淡淡の花香 2024-11-09 13:33:09

首先将>>中的rhs参数中的const去掉修改操作符时。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文