Hanita Hanita - 3 months ago 11
C++ Question

Error:'class MainWindow' has no member named 'insertMessage'

I am writing a SQLite database for client server program.

I wrote insert message function in myserevr.cpp like this:

void myserver::insertMessage(QString IPAddrress, QDate date, QString message)
{
QSqlQuery query;

query.prepare("INSERT INTO messages(IPAddress, date, message)"" values(?,?,?)");

query.addBindValue(IPAddrress);
query.addBindValue(date);
query.addBindValue(message);
query.exec();

qDebug() << query.lastError();
}


and I call it in main.cpp like this :

MainWindow myserver;

bool create = QFile::exists("Message.dat");

if (!myserver.createConnection())
return 1;

if (create)
myserver.insertMessage();


but I get the following error:
"Error:'class MainWindow' has no member named 'insertMessage'"

what should I do? Thanks a lot

Answer

Well , you should not use object name while defining a member of the class instead you should use class name . if insertMessage is the function of class MainWindow then you should not define it using myserver .

 void myserver::insertMessage(QString IPAddrress, QDate date, QString message)//wrong

correct definition should be like below .

void MainWindow::insertMessage(QString IPAddrress, QDate date, QString message)
{
    QSqlQuery query;

    query.prepare("INSERT INTO messages(IPAddress, date, message)"" values(?,?,?)");

    query.addBindValue(IPAddrress);
    query.addBindValue(date);
    query.addBindValue(message);
    query.exec();

    qDebug() << query.lastError();
}

Note : When you are calling inserrMessage , you should call with relevant arguments .

This is how you should call your function .

QString ip;
QDate date;
QString msg;

if (create)
       myserver.insertMessage(ip,date,msg);//no arguments given which is wrong .Pass relevant arguments .