problem with constructor and inheritance QFile object
here is my code:
in _File.h:
Code:
#include <QString>
#include <QFile>
class _File
: public QFile
{
Q_OBJECT
public:
_File();
....//and it goes on
};
in _File.cpp
Code:
_File::_File() :
{
this->setProgress(0);
this->setFilename("");
}
_File
::_File
(const QString &qtname
) {
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
Re: problem with constructor and inheritance QFile object
Your compile error comes because of this code block:
Code:
_File::_File() :
{
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:
Code:
// header
#ifndef myFileClass_H_
#define myFileClass_H_
#include <QFile>
class myFileClass
: public QFile{
Q_OBJECT
public:
};
#endif
// cpp
Code:
#include "myFileClass.h"
myFileClass
::myFileClass(const QString &name
){
// your code comes here
}
myFileClass
::myFileClass(QObject *parent
){
// your code comes here
}
{
// your code comes here
}
myFileClass::~myFileClass()
{
}
Re: problem with constructor and inheritance QFile object
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
Re: problem with constructor and inheritance QFile object
QFile doesn't like to be copied. Store a vector of pointers to QFile instead.
Code:
QVector<QFile*> vecFile;
vecFile = new _File("sample.txt");
Alternatively do this:
Code:
class _File {
public:
_File(){ _file = 0;}
void setFile
(QFile *f
) { _file
= f;
} QFile *file() const { return _file;
} private:
};
QVector<_File> vecFile;
_File f;
f.setFile(...);
vecFile <<f;
Re: problem with constructor and inheritance QFile object
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
Re: problem with constructor and inheritance QFile object
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.
Re: problem with constructor and inheritance QFile object
Quote:
Originally Posted by
wysota
Code:
QVector<QFile*> vecFile;
vecFile = new _File("sample.txt");
i think it should be
Code:
vecFile.push_back(new _File("sample.txt"));
Re: problem with constructor and inheritance QFile object
Quote:
Originally Posted by
MrDeath
i think it should be
Code:
vecFile.push_back(new _File("sample.txt"));
Yes, of course. Actually I meant this:
Code:
vecFile << new _File("sample.txt");