0

I'm using Mysql 5.6 with Qt5 and creating a class to encapsulate a database connection.
When I try to create an object in the main with dbConnect conn = new dbConnect("localhost", "test1","test","user"); I get and error telling me I need a conversion from dbConnect* to a non-scalar type.
Reading other posts I've seen this could be due to the type of inicialization I'm trying because it's not a good practice in C++ and should be something like dbConnect conn("localhost", "test1","test","user"); but, changing it like that, instead of getting the conversion error I get now a no matching function for call to QSqlDatabase::setHosTname(std::string&) and same with the other methods needed for connecting such as setDatabaseName, setUserName and setPassword
Could it be because of the std::string I'm using? Or how should I create the object?
This is m y header db.h:

#ifndef DB
#define DB
#include <QSqlDatabase>
#include <string>
class dbConnect:QSqlDatabase
{
  public:
      dbConnect(std::string host, std::string name,std::string user, std::string pass);
      bool createConnection(std::string host, std::string name,std::string user, std::string pass);
  private:
      QSqlDatabase dbObject;
 };
   #endif // DB_

Next here it is its implementation db.cpp:

#include<QSqlDatabase>
#include <QMessageBox>
#include <QSqlError>

class dbConnect:QSqlDatabase
{
private:
    QSqlDatabase dbObject;
public:
    dbConnect(std::string host, std::string name,std::string user, std::string pass)
    {
        if(createConnection(host, name, user, pass))
        {
            QMessageBox::information(0,"Connection Status","Connection established with database");
        }
        else
        {
             QMessageBox::critical(0,QObject::tr("Error connecting to database"),dbObject.lastError().text());
        }
    }
    bool createConnection(std::string host, std::string name,std::string user, std::string pass)
    {
        dbObject = QSqlDatabase::addDatabase("QMYSQL");
        dbObject.setHostName(host);
        dbObject.setDatabaseName(name);
        dbObject.setUserName(user);
        dbObject.setPassword(pass);
        if(!dbObject.open())
        {
            return false;
        }
        else
        {
            return true;
        }
    }
};

UPDATE

Took @gengisdave and @R Sahu solutions and now I'm trying to create an object in the main. If I try dbConnect conn(); it works fine even if the constructor takes for paremeter but, if I trye dbConnect conn("localhost","test1","test","user"); compiler gives me an error of undefined reference to dbConnect::dbConnect(std::string,std::string,std::string,std::string).

0

2 Answers 2

3

When you use:

dbConnect conn = new dbConnect("localhost", "test1","test","user");

the RHS is of type dbConnect* while the LHS is of type dbConnect. That is not correct. The compiler cannot take a pointer and assign it to an object.

You can use:

dbConnect* connPtr = new dbConnect("localhost", "test1","test","user");

or

dbConnect conn("localhost", "test1","test","user");

Other problems

bool createConnection(std::string host, std::string name,std::string user, std::string pass)
{
    dbObject = QSqlDatabase::addDatabase("QMYSQL");
    dbObject.setHostName(host);
    dbObject.setDatabaseName(name);
    dbObject.setUserName(user);
    dbObject.setPassword(pass);
    if(!dbObject.open())
    {
        return false;
    }
    else
    {
        return true;
    }
}

is not right. You probably need:

bool createConnection(std::string host, std::string name,std::string user, std::string pass)
{
    // Since this class is derived from QSqlDatabase,
    // you can use:
    this->addDatabase("QMYSQL");
    this->setHostName(QString::fromStdString(host));
    this->setDatabaseName(QString::fromStdString(name));
    this->setUserName(QString::fromStdString(user));
    this->setPassword(QString::fromStdString(pass));
    if(!this->open())
    {
        return false;
    }
    else
    {
        return true;
    }
}

Also, remove the member variable

  QSqlDatabase dbObject;

You don't need it since the class is already derived from QSqlDatabase. You'll need to use that only if you don't derive from QSqlDatabase

Sign up to request clarification or add additional context in comments.

Comments

2

QSQlDatabase::setHostName requires a parameter of type QString, while you provide a std::string; the same with the next 3 lines

you can change that lines to

this->setHostName(QString::fromStdString(host));
this->setDatabaseName(QString::fromStdString(name));
this->setUserName(QString::fromStdString(user));
this->setPassword(QString::fromStdString(pass));

EDIT: it works fine

main.cpp

#include <QApplication>
#include "db.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    dbConnect connection("localhost", "test1", "test", "user");
    dbConnect *conn = new dbConnect("localhost", "test1", "test", "user");
}

db.cpp

#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlError>
#include "db.h"

dbConnect::dbConnect(std::string host, std::string name,std::string user, std::string pass)
{
    if ( createConnection(host, name, user, pass) )
        QMessageBox::information(0, "Connection Status", "Connection established with database");
    else
        QMessageBox::critical(0,QObject::tr("Error connecting to database"),dbObject.lastError().text());
}

bool dbConnect::createConnection(std::string host, std::string name,std::string user, std::string pass)
{
    this->dbObject = QSqlDatabase::addDatabase("QMYSQL");
    this->dbObject.setHostName(QString("localhost"));
    this->dbObject.setDatabaseName(QString("test.txt"));
    this->dbObject.setUserName(QString("test"));
    this->dbObject.setPassword(QString("test"));
    return this->dbObject.open();
}

db.h

#ifndef DB_H
#define DB_H

#include <QSqlDatabase>
#include <string>

class dbConnect:QSqlDatabase
{
  public:
    dbConnect(std::string host, std::string name,std::string user, std::string pass);
    bool createConnection(std::string host, std::string name,std::string user, std::string pass);
  private:
    QSqlDatabase dbObject;
};

#endif

compiled with: gcc db.cpp main.cpp -I/usr/include/qt5/QtSql -I/usr/include/qt5/QtWidgets -I/usr/include/qt5 -fPIC -o main -lstdc++ -lQt5Sql -lQt5Core -lQt5Widgets

6 Comments

Thanks dude! I thought from the very beginning I'd have problems with QString, that's why I used std::string but that was the cause of all this.
Ok, not really, now if I create an object of dbConnect like dbConnect conn("localhost", "test1","test","user"); compiler says: `undefined reference to dbConnect::dbConnect(std::string,std::string,std::string,std::string) but, the constructor is declared as it should, isn't it?
you don't need to redefine your class in the .cpp file, change to dbConnect::dbConnect
Changed, and then I get three error as stated: 1. dbConnect::dbConnect names the constructor, not the type 2. expected ';' before conn 3. statement cannot resolve the address of overloaded function. I think the first is the most important because my constructor is not overloaded. I don't really get what type means if I'm just calling the constructor
the first error means you're defining an object using the constructor dbConnect::dbConnect instead of dbConnect type
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.