另一个类中的C ++ \ Qt类对象

时间:2016-04-23 07:28:44

标签: c++ c qt

我在另一个类对象中遇到问题。问题是 - 我的soapserver.cpp函数出现LNK2019错误。但首先要做的是第一件事。

这是我的第一堂课的标题

//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;

};

我也在cpp

定义了set_delivered
    //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

1 个答案:

答案 0 :(得分:1)

如果有更多信息,那将会有所帮助,但是从那里得到的信息......

我认为问题在于你的头文件。

你必须把它放在mysoapserver.cpp文件中:

#include "datalayer.h"


并在mysoapserver.h头文件中输入:

class DataLayer;

说明:

错误Unresolved Symbol指的是DataLayer对象,因为文件mysoapserver.cpp不知道DataLayer是一个东西。至少根据你发布的mysoapserver.cpp

您还需要添加到mysoapserver.cpp

#include "mysoapserver.h"