Results 1 to 8 of 8

Thread: No appropriate default constructor

  1. #1
    Join Date
    Jun 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default No appropriate default constructor

    Hello there, I am legolizard, and I am very new to these forums, yet I would like the reader to note that I have in fact gone over other threads with a similar problem, and found them to be of no help.
    Furthermore, while I am relatively new to Qt, I am not new to C++, and have had a pretty easy time learning all the do-hickies of Qt. However, for some reason no matter what I do I always get "no appropriate default constructor."

    My basic code is the following :

    fileExplorer.hpp :
    Qt Code:
    1. #ifndef FILEEXPLORER_H
    2. #define FILEEXPLORER_H
    3.  
    4. #include <QDialog>
    5. #include <QtCore>
    6. #include <QtGui>
    7.  
    8. namespace Dialog{
    9. class FileExplorer;
    10. }
    11. class FileExplorer : public QDialog{
    12. Q_OBJECT
    13. private:
    14. QFileSystemModel* dirmodel;
    15. QFileSystemModel* filemodel;
    16. Dialog::FileExplorer* dialogbox;
    17. public:
    18. explicit FileExplorer(QWidget* parent = 0 , QString path = "C:\\");//<--This is the !@#$ing default constructor, since I have defaults.
    19. ~FileExplorer();
    20. };
    21.  
    22. #endif // FILEEXPLORER_H
    To copy to clipboard, switch view to plain text mode 
    fileExplorer.cpp:
    Qt Code:
    1. #include "fileExplorer.hpp"
    2. #include "ui_fileExplorer.h"
    3.  
    4. FileExplorer::FileExplorer(QWidget* parent , QString path) : QDialog(parent) , dirmodel(new QFileSystemModel(this)) ,
    5. filemodel(new QFileSystemModel(this)) , dialogbox(new Dialog::FileExplorer){//<--Error here I guess?
    6. dirmodel->setRootPath(path);
    7. dirmodel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
    8.  
    9. filemodel->setRootPath(path);
    10. filemodel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
    11. }
    12. FileExplorer::~FileExplorer(){
    13. delete dirmodel;
    14. delete filemodel;
    15. delete dialogbox;
    16. }
    To copy to clipboard, switch view to plain text mode 
    I really like Qt and find it really easy and powerful, yet this one thing is really bothersome, and I feel stupid because it seems like such a simple thing to fix. x_x
    Any help will be greatly appreciated, thank you. ^.^

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: No appropriate default constructor

    Nothing in the code you have posted is calling the constructor you are cursing about; so that seems unlikely to be your problem.

    Rather than guessing post the actual error message and warning issued by your C++ compiler and tell us which line the error messages actually relate to. It may help to split line 4 and 5 over multiple lines to separate the components to isolate the culprit.

    If the error is vaguely where you guess then it is more likely associated to the Designer UI code. As far as I can see uic is hard-coded to generate a namespace Ui containing the generated class. How have you managed to get uic to generate code for your Designer UI using a namespace Dialog? Best attach ui_fileExplorer.h to your next message too.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: No appropriate default constructor

    I guess the explicit constructor is playing its role. Remove explicit and see if it works.

    Also in some compilers, if you declare a parametrized constructor, you are supposed to declare a default constructor too. And thats C++, not Qt.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: No appropriate default constructor

    I guess the explicit constructor is playing its role.
    No, this compiles without error:
    Qt Code:
    1. class Test {
    2. public:
    3. explicit Test(int a = 0, double b = 1.0) {}
    4. };
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. Test t; // default constructor
    9. Test u(1); // one optional arg, conversion constructor
    10. Test v(1, 2.0); // full constructor
    11. t = Test(8);
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 
    as it should. The "explicit" will stop this compiling though:
    Qt Code:
    1. t = 5
    To copy to clipboard, switch view to plain text mode 
    because it blocks the implicit conversion from int to Test using the constructor, i.e. the Test constructor must be called explicitly.

  5. #5
    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: No appropriate default constructor

    Quote Originally Posted by legolizard View Post
    I really like Qt and find it really easy and powerful, yet this one thing is really bothersome, and I feel stupid because it seems like such a simple thing to fix.
    Yet this has nothing to do with Qt and is strictly a C++ issue

    Unless you tell us the exact error you get, there is not much we can do about it.
    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.


  6. #6
    Join Date
    Jun 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: No appropriate default constructor

    Nothing in the code you have posted is calling the constructor you are cursing about; so that seems unlikely to be your problem.
    Um actually I do, in the initalization list of FileExplorer, I go :
    Qt Code:
    1. dialog(new Dialog::FileExplorer)
    To copy to clipboard, switch view to plain text mode 
    Thus calling the constructor, and in fact it is the error from the compiler, thank you though, as I didn't know that the names where hard coded by Qt, and that is actually the problem. I have looked through the ui_fileExplorer.h and found that both the class and the namespace are supposed to be prenamed to Ui, and Dialog, the only reason I got it to be my own names was simply because I had memorized the pre-code and wrote it all myself. I don't much like the computer writing my code. :/

    Also in some compilers, if you declare a parametrized constructor, you are supposed to declare a default constructor too. And thats C++, not Qt.
    I don't know what compilers you are talking about, but in the one I am using(and most others I have used) if one gives defaults(as I have) then the function, even though it has parameters is technically considered to be the default constructor.

    Yet this has nothing to do with Qt and is strictly a C++ issue
    Actually it does, as I have stated, Qt does not recognize my names and it must be named what it wants, with the Ui namespace and a class within called Dialog(since the whole thing is a subclass of QDialog).

    Again thank you, for your help, and next time I have a bug I will be sure to post the error as welll.

  7. #7
    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: No appropriate default constructor

    Quote Originally Posted by legolizard View Post
    as I didn't know that the names where hard coded by Qt, and that is actually the problem. I have looked through the ui_fileExplorer.h and found that both the class and the namespace are supposed to be prenamed to Ui, and Dialog, the only reason I got it to be my own names was simply because I had memorized the pre-code and wrote it all myself. I don't much like the computer writing my code. :/
    Are you serious with this?

    Actually it does, as I have stated, Qt does not recognize my names
    "Qt" is a library. It is not meant to "recognize names". It is meant to provide functions and classes. Your C++ compiler is meant to "recognize names" thus the issue is related to your ability of using the C++ language.

    it must be named what it wants, with the Ui namespace and a class within called Dialog(since the whole thing is a subclass of QDialog).
    No, not really. You don't have to use the Ui namespace. Yes, you have to use the class name since you are trying to access the constructor of that class -- again, nothing Qt specific, regular C++.
    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.


  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: No appropriate default constructor

    Quote Originally Posted by legolizard View Post
    Um actually I do, in the initalization list of FileExplorer, I go :
    Qt Code:
    1. dialog(new Dialog::FileExplorer)
    To copy to clipboard, switch view to plain text mode 
    Thus calling the constructor, and in fact it is the error from the compiler,
    Um actually you don't. The constructor for your class FileExplorer in the global namespace, you the one you curse about, is not called in your code. Your counter example is attempting to call the default constructor of a class FileExplorer in a namespace called Dialog. This is not the same class. This is pure C++ and nothing to do with Qt.

    The attempt to compile and link a call to the Dialog::FileExplorer constructor was likely failing because there is no class Dialog::FileExplorer definition beyond the minimal forward declaration. Attempting to do something similar here generates a slew of (different) error messages from my compiler. This, also, is pure C++.

    If you want to use generated code then you have to use the code as generated: Qt is not magic, it cannot read your mind and automatically generate code to match identifiers you have invented. The namespace Ui is fixed in uic, but the name of the class within that space is user-definable using the object name you provide in Designer. If you have an ideological objection with your computer generating code for you then I suggest you stop using Designer and uic, Qt does not force these upon you. You may also wish to stop using qmake which, after all, is only generating a Makefile for you.

Similar Threads

  1. Replies: 13
    Last Post: 19th June 2011, 21:04
  2. Help with QSettings default constructor
    By Polnareff in forum Qt Programming
    Replies: 1
    Last Post: 1st October 2010, 18:27
  3. QtScript: default constructor question
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2009, 19:36
  4. Q3Frame : no appropraite default constructor available
    By Project25 in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2007, 23:23
  5. What default constructor?
    By bitChanger in forum General Programming
    Replies: 5
    Last Post: 15th February 2006, 19:50

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.