嗨我有类对象的问题,也许你可以解决它。那是我的代码:
Plugin.h:
class DemandPlugin
{
public:
QString sendtodb(QSqlDatabase &db, QString &cache, QString function);
QSqlDatabase db;
DemandPlugin();
virtual ~DemandPlugin();
};
Plugin.cpp:
DemandPlugin::DemandPlugin()
{
if (!db.open())
{
GlobalLog::log("SqlSource", "ERROR: Cannot connect to db!");
}
this->selectedPath = QString::null;
/*
here are things which I don't want to show You, sorry ;)
*/
}
QString DemandPlugin::sendtodb(QSqlDatabase &db, QString &cache, QString function)
{
QSqlQuery query(function,db);
query.next();
cache = query.value(0).toString();
return cache;
}
DemandPlugin::~DemandPlugin() { }
上面这两个文件可以正常使用,它们位于我项目的一个位置(Project / Subs / Demands / plugin.h,Project / Subs / Demands / plugin.cpp)。问题出在第三个问题上。这个名为task.cpp的文件位于项目/ Api / capi / task.cpp中。
我想使用“插件”中定义的类对象,但它返回错误: '对'DemandPlugin :: sendtodb(QSqlDatabase&,QString&,QString)'的未定义引用。
#include "../../Subs/Demands/plugin.h"
DemandPlugin *demand_plugin = new DemandPlugin();
double Task::getTotalCost()
{
//return (costCoef * cost + baseCost) * waitCoef;
// //
QString hostname = GlobalConfig::getValue("dbCredentials.ini", "database/host").toString();
QString database = GlobalConfig::getValue("dbCredentials.ini", "database/db_name").toString();
QString username = GlobalConfig::getValue("dbCredentials.ini", "database/user").toString();
QString password = GlobalConfig::getValue("dbCredentials.ini", "database/password").toString();
QString connectionName = QString::number((int)this, 16);
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QPSQL", connectionName);
db.setHostName(hostname);
db.setDatabaseName(database);
db.setUserName(username);
db.setPassword(password);
QString Worker_Name = GlobalConfig::getValue("cloud.ini", "name").toString();
QString tbname;
QString value;
double infinity = std::numeric_limits<float>::infinity();
QList <QString> tagnamevalue_tag;
double new_cost;
if(Worker_Name.contains("Luke") ==true)
{
QString line1 = "SELECT TAGS FROM TEST WHERE SUITE_ID = '"+ getParam("subs_test_id") +"'";
tagnamevalue_tag = demand_plugin->sendtodb(db, value, line1).split(" ");
QString part_of_query;
for (int q=0; q<tagnamevalue_tag.count(); q++)
{
part_of_query = part_of_query +"AND tags LIKE '%"+tagnamevalue_tag[q]+"%'";
}
QString line3 = "UPDATE Test SET selected = 'YES' WHERE id = (SELECT id FROM Testbays WHERE name LIKE '%Luke%' AND selected='NO'"+part_of_query+" ORDER BY id LIMIT 1) RETURNING name;";
tbname = demand_plugin->sendtodb(db, value, line3);
if(tbname == NULL)
{
new_cost = infinity;
}
else
{
new_cost = (costCoef * cost + baseCost) * waitCoef;
}
}
return new_cost;
}