Results 1 to 8 of 8

Thread: problem with constructor and inheritance QFile object

  1. #1
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default problem with constructor and inheritance QFile object

    here is my code:

    in _File.h:

    Qt Code:
    1. #include <QString>
    2. #include <QFile>
    3.  
    4. class _File : public QFile
    5.  
    6.  
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. _File();
    12. _File(const QString &name);
    13. ....//and it goes on
    14. };
    To copy to clipboard, switch view to plain text mode 

    in _File.cpp


    Qt Code:
    1. _File::_File() :
    2.  
    3. {
    4. this->setProgress(0);
    5.  
    6. this->setFilename("");
    7. }
    8.  
    9.  
    10. _File::_File(const QString &qtname)
    11. : QFile(name)
    12. {
    13.  
    14. this->setProgress(0);
    15. ...}}
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by wysota; 24th January 2011 at 14:42. Reason: missing [code] tags

  2. #2
    Join Date
    Sep 2010
    Location
    Germany
    Posts
    28
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: problem with constructor and inheritance QFile object

    Your compile error comes because of this code block:

    Qt Code:
    1. _File::_File() :
    2. {
    3. this->setProgress(0);
    4. this->setFilename("");
    5. }
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. // header
    2. #ifndef myFileClass_H_
    3. #define myFileClass_H_
    4.  
    5. #include <QFile>
    6.  
    7. class myFileClass : public QFile
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. QFile ( const QString & name )
    13. QFile ( QObject * parent )
    14. QFile ( const QString & name, QObject * parent )
    15. ~QFile ()
    16. };
    17.  
    18. #endif
    To copy to clipboard, switch view to plain text mode 

    // cpp
    Qt Code:
    1. #include "myFileClass.h"
    2.  
    3. myFileClass::myFileClass(const QString &name)
    4. : QFile(name)
    5. {
    6. // your code comes here
    7. }
    8.  
    9. myFileClass::myFileClass(QObject *parent)
    10. : QFile(parent)
    11. {
    12. // your code comes here
    13. }
    14.  
    15.  
    16. myFileClass::myFileClass(const QString &name, QObject *parent)
    17. : QFile(name, parent)
    18. {
    19. // your code comes here
    20. }
    21.  
    22. myFileClass::~myFileClass()
    23. {
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 24th January 2011 at 14:44. Reason: removed 3rd party storage url, pasted code inline

  3. #3
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default 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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with constructor and inheritance QFile object

    QFile doesn't like to be copied. Store a vector of pointers to QFile instead.
    Qt Code:
    1. QVector<QFile*> vecFile;
    2. vecFile = new _File("sample.txt");
    To copy to clipboard, switch view to plain text mode 

    Alternatively do this:
    Qt Code:
    1. class _File {
    2. public:
    3. _File(){ _file = 0;}
    4. void setFile(QFile *f) { _file = f; }
    5. QFile *file() const { return _file; }
    6. private:
    7. QFile *_file;
    8. };
    9.  
    10. QVector<_File> vecFile;
    11. _File f;
    12. f.setFile(...);
    13. vecFile <<f;
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default 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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem with constructor and inheritance QFile object

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. QVector<QFile*> vecFile;
    2. vecFile = new _File("sample.txt");
    To copy to clipboard, switch view to plain text mode 
    i think it should be
    Qt Code:
    1. vecFile.push_back(new _File("sample.txt"));
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with constructor and inheritance QFile object

    Quote Originally Posted by MrDeath View Post
    i think it should be
    Qt Code:
    1. vecFile.push_back(new _File("sample.txt"));
    To copy to clipboard, switch view to plain text mode 
    Yes, of course. Actually I meant this:
    Qt Code:
    1. vecFile << new _File("sample.txt");
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Object and multiple inheritance for interfaces
    By brcain in forum Qt Programming
    Replies: 8
    Last Post: 29th June 2021, 15:29
  2. Replies: 4
    Last Post: 20th August 2010, 13:07
  3. Inheritance problem
    By brevleq in forum Qt Programming
    Replies: 6
    Last Post: 23rd December 2008, 06:27
  4. problem in widget inheritance
    By wagmare in forum Qt Programming
    Replies: 2
    Last Post: 10th December 2008, 07:35
  5. Replies: 4
    Last Post: 12th August 2008, 01:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.