我在使用另一個類對象時遇到問題。問題是 - 我在我的soapserver.cpp函數中有LNK2019錯誤。但首先是第一件事。另一個類中的C++ Qt類對象
這裏是
//datalayer.h
#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>
class DataLayer : public QObject
{
Q_OBJECT
public:
bool set_delivered(const QString& UCID, const QString& Key, QVariant &result);
explicit DataLayer(QObject *parent = 0);
QString getLastError() const{return lastError;}
private:
QString lastError;
};
我也set_delivered在CPP
//datalayer.cpp
DataLayer::DataLayer(QObject *parent) : QObject(parent)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","DB");
//db.databaseName();
//db.hostName();
//db.userName();
//db.password();
q = new QSqlQuery(db);
//q->prepare(""); //not implemented yet
}
DataLayer::~DataLayer()
{
QSqlDatabase db = QSqlDatabase::database("DB",false);
if(db.open())
{
db.close();
}
QSqlDatabase::removeDatabase("DB");
}
bool DataLayer::set_delivered(const QString &UCID, const QString &Key, QVariant &result)
{
result.clear();
QSqlDatabase db = QSqlDatabase::database("DB",false);
if(!db.open())
{
lastError = db.lastError().text();
return false;
}
q->prepare("INSERT INTO ActiveCall VALUES(:UCID,:Time,NULL,NULL,:Key)");
q->bindValue(":UCID",UCID);
q->bindValue(":Time",QTime::currentTime().toString("HH:mm:ss:zzz"));
q->bindValue(":Key",Key);
if(!q->exec())
{
lastError = q->lastError().text();
return false;
}
q->next();
result = q->value("Time").toString();
db.close();
return true;
}
讓我們跳向另一個類中定義我的第一類的頭,mysoapserver
//mysoapserver.h
#include "soap/soapKursWebService.h"
#include "soap/soapH.h"
#include <QObject>
#include <QSqlQuery>
#include <qdebug.h>
#include "datalayer.h"// I'm including datalayer header here
class MySOAPServer : public QObject, public KursWebService
{
Q_OBJECT
public:
MySOAPServer(QObject* parent = 0);
int delivered(int UCID, int DeviceNum, int &result);
private:
DataLayer* r;// creating of the class object
};
而且,主要部分,此類的cpp
//mysoapserver.cpp
int MySOAPServer::delivered(int UCID, int DeviceNum, int &result)//вызов доставлен, но трубку не снял, в табличку время начала
{
qDebug()<<"delivered "<<UCID<<" "<< DeviceNum;
QVariant res = 0;
QString UCID_S = QString::number(UCID);
QString Key_S = QString::number(DeviceNum);
bool ok = r->set_delivered(UCID_S,Key_S,res);//LNK2019 here
if(!ok)
result = r->getLastError().toInt();//magicly works here
else
result = res.toInt();
return SOAP_OK;
}
我知道,我對類對象做了一些愚蠢的事情,但我現在已經失去知覺了。預先感謝您的幫助!
編輯:加入我的.pro文件和我的datalayer.cpp
親文件:如果有一點點的信息
#-------------------------------------------------
#
# Project created by QtCreator 2016-04-19T02:23:22
#
#-------------------------------------------------
QT += core gui network sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Server_MVSC_Kurs_III
TEMPLATE = app
DEFINES += WITH_PURE_VIRTUAL
INCLUDEPATH += \
C:/Qt/gsoap-2.8.15/gsoap/
SOURCES += main.cpp\
mainwindow.cpp \
ipcserver.cpp \
mysoapserver.cpp \
soap/soapC.cpp \
soap/soapKursWebService.cpp \
soap/stdsoap2.cpp \
datalayer.cpp
HEADERS += mainwindow.h \
ipcserver.h \
mysoapserver.h \
soap/soapH.h \
soap/soapKursWebService.h \
soap/soapStub.h \
soap/stdsoap2.h \
soap/KursWeb.nsmap \
datalayer.h
FORMS += mainwindow.ui
什麼是您所遇到的問題? –
我認爲mysoapserver.cpp中的'LNK2019 here'(未解析的外部符號)需要一個解決方案。 – user3188346
顯示您的項目文件。您應該將您的DataLayer模塊鏈接到項目中才能使用。 2顯示你的DataLayer.cpp完全 – VladimirLenin