Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

[SOLVED] Qt Serialization

qt4 serialization c++ qdatastream

  • This topic is locked This topic is locked
2 replies to this topic

#1 mctim


    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

Posted 20 May 2012 - 04:51 PM

I'm trying to serialize some objects and I cant get it to work correctly. I'm using qt4 i believe. I tried following a tutorial on Stackoverflow, however ,I'm getting compiler errors. This error more specifically:


/home/andy/MOB-build-desktop-Qt_4_8_1_in_PATH__System__Release/../MOB/user.cpp:88: error: passing 'const User' as 'this' argument of 'User& User::operator=(const User&)' discards qualifiers [-fpermissive]


I don't see what I'm doing wrong would anyone care to take a look at it?

Here's my header file.

#ifndef USER_H
#define USER_H
#include "MOB_global.h"
#include <string.h>
#include <qstring.h>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
using namespace std;
User(const string&, const string&);
User(const string&, const string&,
const string&, const string&);
string GetName() const;
virtual void SetName(string);
string GetPass() const;
virtual void SetPass(string);
string GetLastName() const;
virtual void SetLastName(string);
string GetLastPass() const;
virtual void SetLastPass(string);
virtual ~User();
QString* userName;
QString* password;
QString* lastFmName;
QString* lastFmPass;
QDataStream &operator >>(QDataStream &in, const User &u);
QDataStream &operator <<(QDataStream &out, const User &u);
#endif // USER_H

Here's my source:

#include "user.h"
User::User(const string& name, const string& pass)
userName = new QString();
*userName = QString::fromStdString(name);
password = new QString();
*password = QString::fromStdString(pass);
lastFmName = NULL;
lastFmPass = NULL;
User::User(const string& userNam, const string& userPass,
const string& lastName, const string& lastPass)
userName = new QString();
*userName = QString::fromStdString(userNam);
password = new QString();
*password = QString::fromStdString(userPass);
lastFmName = new QString();
*lastFmName = QString::fromStdString(lastName);
lastFmPass = new QString();
*lastFmPass = QString::fromStdString(lastPass);
User::User() {
userName = NULL;
password = NULL;
lastFmName = NULL;
lastFmPass = NULL;
delete userName;
delete password;
delete lastFmName;
delete lastFmPass;
string User::GetName() const {
return userName->toStdString();
void User::SetName(string newName) {
*userName = QString::fromStdString(newName);
void User::SetPass(string newPass) {
*password = QString::fromStdString(newPass);
string User::GetPass() const {
return password->toStdString();
string User::GetLastName() const {
return lastFmName->toStdString();
void User::SetLastName(string newLast) {
*lastFmName = QString::fromStdString(newLast);
string User::GetLastPass() const {
return lastFmPass->toStdString();
void User::SetLastPass(string newPass) {
*lastFmPass = QString::fromStdString(newPass);
QDataStream &operator <<(QDataStream &out, const User &u) {
out << QString::fromStdString(u.GetName())
<< QString::fromStdString(u.GetPass())
<< QString::fromStdString(u.GetLastName())
<< QString::fromStdString(u.GetLastPass());
return out;
QDataStream &operator >>(QDataStream &in, const User& u) {
QString username;
QString password;
QString lastname;
QString lastpass;
in >> username >> password >> lastname >> lastpass;
u = User(username.toStdString(), password.toStdString(),
lastname.toStdString(), lastpass.toStdString());
return in;
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive & ar, User & u, const unsigned int version)
ar << u.userName;
ar << u.password;
ar << u.lastFmName;
ar << u.lastFmPass;
} // namespace serialization
} // namespace boost

Edited by Roger, 19 February 2013 - 03:28 PM.

  • 0

#2 kernelcoder


    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 20 May 2012 - 06:06 PM

Even if there were no compiling errors in your code, there are catches in your code. As example, you have kept pointer of string for 4 string type (userName, password, lastFmName & lastFmPass) and you are serializing the pointer (not their value) in the Arvhice class. Is that you want? I think you don't need to keep the pointer for those 4 string. So, whenever you'll specialize the Archive template class or, you'll create an instance of this class by specializing it, you'll get compiler errors.

However, the error you are getting in method (actually in while overloading the >> operator) 'QDataStream &operator >>(QDataStream &in, const User& u)'. Here you are assigning an instance of User to a const reference of User (the u). The reference itself contains a reference to a constant pointer (that is in 'User& u' u actually reference to a pointer of type like User const * u) -- so you can't change a reference. That causes the error. The best practice for >> operator overriding is that you need to pass a reference of non-const type. So if you change your code for 'QDataStream &operator >>(QDataStream &in, User &u);' operator overriding as follows, it will work for you. You need to remove the constness because you need to change the fields for User class inside the >> operator overloading.

In user.h header file
// Add this line at the top of user.h to include header file for QDataStream
#include <QDataStream>

// In the declaration for >> operator overloading
QDataStream &operator >>(QDataStream &in, User &u);  // removed the const keyword form 'const User& u'
QDataStream &operator <<(QDataStream &out, const User &u);

User.cpp file
// in the cpp file change the body for >> operator overloading as this
QDataStream &operator >>(QDataStream &in, User& u)		{
		QString username;
		QString password;
		QString lastname;
		QString lastpass;
		in >> username >> password >> lastname >> lastpass;
		return in;

  • 0

#3 mctim


    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

Posted 20 May 2012 - 08:05 PM

I'm not sure if it was what was mentioned in the previous post but I was able to fix my issue by removing a const keyword
  • 0

Also tagged with one or more of these keywords: qt4, serialization, c++, qdatastream

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download