PDA

View Full Version : problem with constructor and inheritance QFile object



naturalpsychic
24th January 2011, 13:32
here is my code:

in _File.h:


#include <QString>
#include <QFile>

class _File : public QFile


{
Q_OBJECT

public:
_File();
_File(const QString &name);
....//and it goes on
};

in _File.cpp



_File::_File() :
QFile()

{
this->setProgress(0);

this->setFilename("");
}


_File::_File(const QString &qtname)
: QFile(name)
{

this->setProgress(0);
...}}

and it goes on

but it has got compilation errors now. it says :
QFile::QFile(const QFile&) is private

within this context; when i click "within this context" it takes me to starting braces of _File class in header file


I am so over this error now, i have been trying to resolve this error for about 2 hours now...any help would be appreciated. thanks

Sven
24th January 2011, 14:09
Your compile error comes because of this code block:



_File::_File() :
QFile()
{
this->setProgress(0);
this->setFilename("");
}


You are trying to use a QFile constructor which is not allowed because it is private. (There is no public default QFile constructor)

Why do you don't use something like this:

// header
#ifndef myFileClass_H_
#define myFileClass_H_

#include <QFile>

class myFileClass : public QFile
{
Q_OBJECT

public:
QFile ( const QString & name )
QFile ( QObject * parent )
QFile ( const QString & name, QObject * parent )
~QFile ()
};

#endif

// cpp

#include "myFileClass.h"

myFileClass::myFileClass(const QString &name)
: QFile(name)
{
// your code comes here
}

myFileClass::myFileClass(QObject *parent)
: QFile(parent)
{
// your code comes here
}


myFileClass::myFileClass(const QString &name, QObject *parent)
: QFile(name, parent)
{
// your code comes here
}

myFileClass::~myFileClass()
{
}

naturalpsychic
24th January 2011, 16:43
I completely understood what you mean but what i am trying to do is: later on somewhere in code,
i try doing this:

QVector<_File> vecFile;

...
...

vecFile=_File("sample.txt");
...
...


this is exactly where problem is, it says copy constructor is private and also can't use = operator because it is private as well. How would i write my copy constructor in way so it can be initiated as mentioned above?

Thanks for reply

wysota
24th January 2011, 18:40
QFile doesn't like to be copied. Store a vector of pointers to QFile instead.

QVector<QFile*> vecFile;
vecFile = new _File("sample.txt");

Alternatively do this:

class _File {
public:
_File(){ _file = 0;}
void setFile(QFile *f) { _file = f; }
QFile *file() const { return _file; }
private:
QFile *_file;
};

QVector<_File> vecFile;
_File f;
f.setFile(...);
vecFile <<f;

naturalpsychic
25th January 2011, 06:44
finally i have done it, putting up for someone else's help:
what i did is i overridden operator= and created costructor _File(const _File&) and this solved my problem now i can assign other class's values to my _File class

i can do
_File f=_File("qtforum.txt");

thanks for replies guys

wysota
25th January 2011, 09:11
This is a very bad idea, someone who suggested you the idea didn't know what he was talking about. QFile is a QObject which can't be copied around safely. Do not do this or sooner or later you will run into problems. QObjects are identity based and not value based, don't copy them. There is a point why operator= and copy constructor of all QObject subclasses are private.

nish
25th January 2011, 10:12
QVector<QFile*> vecFile;
vecFile = new _File("sample.txt");

i think it should be

vecFile.push_back(new _File("sample.txt"));

wysota
25th January 2011, 10:15
i think it should be

vecFile.push_back(new _File("sample.txt"));
Yes, of course. Actually I meant this:

vecFile << new _File("sample.txt");