PDA

View Full Version : No appropriate default constructor



legolizard
19th June 2012, 03:53
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 :


#ifndef FILEEXPLORER_H
#define FILEEXPLORER_H

#include <QDialog>
#include <QtCore>
#include <QtGui>

namespace Dialog{
class FileExplorer;
}
class FileExplorer : public QDialog{
Q_OBJECT
private:
QFileSystemModel* dirmodel;
QFileSystemModel* filemodel;
Dialog::FileExplorer* dialogbox;
public:
explicit FileExplorer(QWidget* parent = 0 , QString path = "C:\\");//<--This is the !@#$ing default constructor, since I have defaults.
~FileExplorer();
};

#endif // FILEEXPLORER_H

fileExplorer.cpp:


#include "fileExplorer.hpp"
#include "ui_fileExplorer.h"

FileExplorer::FileExplorer(QWidget* parent , QString path) : QDialog(parent) , dirmodel(new QFileSystemModel(this)) ,
filemodel(new QFileSystemModel(this)) , dialogbox(new Dialog::FileExplorer){//<--Error here I guess?
dirmodel->setRootPath(path);
dirmodel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);

filemodel->setRootPath(path);
filemodel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
}
FileExplorer::~FileExplorer(){
delete dirmodel;
delete filemodel;
delete dialogbox;
}

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. ^.^

ChrisW67
19th June 2012, 05:12
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.

aamer4yu
19th June 2012, 06:54
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.

ChrisW67
19th June 2012, 07:04
I guess the explicit constructor is playing its role.
No, this compiles without error:


class Test {
public:
explicit Test(int a = 0, double b = 1.0) {}
};

int main(int argc, char **argv)
{
Test t; // default constructor
Test u(1); // one optional arg, conversion constructor
Test v(1, 2.0); // full constructor
t = Test(8);
return 0;
}

as it should. The "explicit" will stop this compiling though:

t = 5
because it blocks the implicit conversion from int to Test using the constructor, i.e. the Test constructor must be called explicitly.

wysota
19th June 2012, 10:31
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.

legolizard
19th June 2012, 13:35
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 :

dialog(new Dialog::FileExplorer) 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.

wysota
19th June 2012, 13:47
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++.

ChrisW67
19th June 2012, 23:58
Um actually I do, in the initalization list of FileExplorer, I go :

dialog(new Dialog::FileExplorer) 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.