I have created a class (MyNodeClass) and now want to add serialization capabilities. I declare (in mynodeclass.h) my serialization methods as follows:
#ifndef MYNODECLASS_H
#define MYNODECLASS_H
#include <QString>
#include <QVariant>
#include <QDataStream>
#include <QReadWriteLock>
namespace NS { class MyNodeClass; }
QDataStream& operator<<(QDataStream &out, const MyNodeClass &node);
QDataStream& operator>>(QDataStream &in, MyNodeClass &node);
namespace NS {
class MyNodeClass
{
public:
MyNodeClass(quint64 parent, QString name, MyNodeClass::MyEnum type);
friend QDataStream& operator<<(QDataStream &out, const MyNodeClass &node);
friend QDataStream& operator>>(QDataStream &in, MyNodeClass &node);
private:
quint64 m_parent;
QString m_name;
int m_type;
QVariant m_content;
QReadWriteLock m_lock;
};
}
#endif // MYNODECLASS_H
My definitions (in mynodeclass.cpp) are:
#include "mynodeclass.h"
QDataStream& operator<<(QDataStream &out, const NS::MyNodeClass &node) {
out
<< node.m_name
<< node.m_type
<< node.m_parent
<< node.m_content;
return out;
}
QDataStream& operator>>(QDataStream &in, NS::MyNodeClass &node) {
QString name;
int type;
quint64 parent;
QVariant contents;
in
>> name
>> type
>> parent
>> contents;
node = NS::MyNodeClass(parent,name,type);
return in;
}
using namespace NS;
MyNodeClass::MyNodeClass(quint64 parent, QString name, MyNodeClass::MyEnum type) {}
The compiler complains that my operator<< cannot access private members of NS::MyNodeClass. That means the signatures aren't lining up. I've tried every combination of namespace related changes but can't figure it out. I've modelled my code after this post
How should the friend functions be defined so that there is no complaint about namespace or access to private members? (Please note that all examples I could find use a single file, so splitting into .h and .cpp is part of what needs to be addressed)
You declare 2
operator >>
in different namespace, only the one innamespace NS
isfriend
.get rid of your "forward declarations":
and implement
NS::operator<<
: