Good afternoon,
I guess it is more a c++ beginner's question than actually Qt releated. So it probably fits best here:
I want to access a MySql database. Everything is working fine. To get a better structure I wanted to have my database interactions in one class:
MyDatabase
Ok, so why not inherit from QSqlDatabase and extend with the needed additional members:
#ifndef MYDATABASE_H
#define MYDATABASE_H
#include <QSqlDatabase>
{
public:
MyDatabase();
};
#endif // MYDATABASE_H
#ifndef MYDATABASE_H
#define MYDATABASE_H
#include <QSqlDatabase>
class MyDatabase : public QSqlDatabase
{
public:
MyDatabase();
};
#endif // MYDATABASE_H
To copy to clipboard, switch view to plain text mode
If I now want to create a connection with:
mydb = MyDatabase::addDatabase("QMYSQL")
mydb = MyDatabase::addDatabase("QMYSQL")
To copy to clipboard, switch view to plain text mode
I get the message:
./Stromverbrauch/mainwindow.cpp: In member function 'int MainWindow::connectDatabase()':
../Stromverbrauch/mainwindow.cpp:33:44: error: no match for 'operator=' in '((MainWindow*)this)->MainWindow::mydb = QSqlDatabase::addDatabase(const QString&, const QString&)((* & QString((*(const QLatin1String*)(& QLatin1String(QSqlDatabase::defaultConnection))))))'
../Stromverbrauch/mainwindow.cpp:33:44: note: candidate is:
In file included from ../Stromverbrauch/mainwindow.h:25:0,
from ../Stromverbrauch/mainwindow.cpp:1:
../Stromverbrauch/mydatabase.h:6:7: note: MyDatabase& MyDatabase::operator=(const MyDatabase&)
../Stromverbrauch/mydatabase.h:6:7: note: no known conversion for argument 1 from 'QSqlDatabase' to 'const MyDatabase&'
./Stromverbrauch/mainwindow.cpp: In member function 'int MainWindow::connectDatabase()':
../Stromverbrauch/mainwindow.cpp:33:44: error: no match for 'operator=' in '((MainWindow*)this)->MainWindow::mydb = QSqlDatabase::addDatabase(const QString&, const QString&)((* & QString((*(const QLatin1String*)(& QLatin1String(QSqlDatabase::defaultConnection))))))'
../Stromverbrauch/mainwindow.cpp:33:44: note: candidate is:
In file included from ../Stromverbrauch/mainwindow.h:25:0,
from ../Stromverbrauch/mainwindow.cpp:1:
../Stromverbrauch/mydatabase.h:6:7: note: MyDatabase& MyDatabase::operator=(const MyDatabase&)
../Stromverbrauch/mydatabase.h:6:7: note: no known conversion for argument 1 from 'QSqlDatabase' to 'const MyDatabase&'
To copy to clipboard, switch view to plain text mode
Ok, I have read somewhere in a different forum the advice to reimplement the "addDatabase" function.
But why is this necessary? Shoud the child not inheric also static functions?
What am I missing to understand the issue?
I am looking forward for your advice.
RogerWilco77
Bookmarks