Solved Default agument missing?
-
but if I do:
explicit BaseDatosPrincipal(QWidget *parent = 0, QSqlDatabase db1=0);
or
explicit BaseDatosPrincipal(QWidget *parent = 0, QSqlDatabase db1=NULL);
could not convert 0 from int to QSqlDatabase
So. What value goes there?
-
So. What value goes there?
A valid instance of QSqlDatabase
BaseDatosPrincipal(QWidget *parent = 0, QSqlDatabase db1=QSqlDatabase());
-
Or change the parameter from type from
QSqlDatabase
toQSqlDatabase*
. I don't know much about the specifics of QSqlDatabase but I don't think you can assign an int or NULL value to an object. -
@Julian See http://en.cppreference.com/w/cpp/language/default_arguments:
int f(int = 1, int); // error, assuming there's no previous declaration of f
because "In a function declaration, after a parameter with a default argument, all subsequent parameters must
have a default argument supplied in this or a previous declaration;[...]"You can write
explicit BaseDatosPrincipal(QSqlDatabase db1, QWidget *parent = 0);
if you don't want the database to have a default value. If you give QWidget *parent = 0 first, all subsequent parameters must have default arguments.
-
Hi,
@mpergand This is an invalid QSqlDatabse object like explained in the constructor documentation.
@Julian Are you using multiple database connections in your software ?
-
This is an invalid QSqlDatabse object like explained in the constructor documentation.
You're right, the object is invalid, but the instance is valid/created, this is what the method parameter need and it's what I want to show.
But if the default constructor create an invalid object, why is this constructor public ? -
There's nothing wrong with a constructor creating an invalid object. This just means that you have to do some more work to have a valid object.
But usually with QSqlDatabase, you don't proceed like that.
-
It's obvious that Julian wants to declare a pointer instead, but it's not what he did and asked for :)
-
Two possibilities:
- Only one connection used in the app
- Several different connections
For 1, there's absolutely no need to copy around QSqlDatabase objects, just enjoy the default connection and its benefits.
For 2, pass the name of the connection around and use QSqlDatabase::database to get the corresponding QSqlDatabase object when needed.
No need for any QSqlDatabase pointers or member variables.
-
@SGaist Are you using multiple database connections in your software ?
Yes, a lot.
@mpergand It's obvious that Julian wants to declare a pointer instead, but it's not what he did and asked for :)
I want to pass a pointer or by copy (like the frist post) to an open database to do not have to open it again, with other connection.
Pass a pointer it's more faster than pass the value (by copy)?
I what @mpergand said in the first post work. But I want to do in the fastest way.
-
Then do as I suggest for option 2. Once a connection is opened, it stays opened until either
- You close it
- There's server/network problem if you are using a remote database. i.e. not SQLite.
Having copies of QSqlDatabase objects lying around everywhere in your code doesn't make it any simpler and you are going to see a lot of warning saying the you are destroying a database while connections are still in use.