我需要帮助找出该代码的错误之处:
class DatabaseEngine
{
protected:
DatabaseEngine();
static DatabaseEngine* m_DatabaseEngine;
public:
static DatabaseEngine& instance();
void do_something();
};
cpp:
#include "databaseengine.h"
DatabaseEngine* DatabaseEngine::m_DatabaseEngine=nullptr;
DatabaseEngine::DatabaseEngine()
{
}
static DatabaseEngine& DatabaseEngine:: instance()
{
if(m_DatabaseEngine==nullptr)
{
m_DatabaseEngine=new DatabaseEngine;`enter code here`
}
return *m_DatabaseEngine;
}
void DatabaseEngine::do_something()
{
}
userwindow.cpp:
#include "databaseengine.h"
UsersWindow::UsersWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::UsersWindow)
{
ui->setupUi(this);
DatabaseEngine::instance().do_something();
}
UsersWindow::~UsersWindow()
{
delete ui;
}
userswindow.obj:-1:错误:LNK2019:未解决的外部符号“ public:静态类DatabaseEngine&__cdecl DatabaseEngine :: instance(void)”(?instance @ DatabaseEngine @@ SAAAV1 @ XZ)在函数“ public:__thiscall UsersWindow中引用” :UsersWindow(QWidget *类)“(?? 0UsersWindow @@ QAE @ PAVQWidget @@@ Z)
userswindow.obj:-1:错误:LNK2019:未解决的外部符号“ public:void __thiscall DatabaseEngine :: do_something(void)”(?do_something @ DatabaseEngine @@ QAEXXZ)在函数“ public:__ thiscall中,UsersWindow :: UsersWindow(类QWidget) *)“(?? 0UsersWindow @@ QAE @ PAVQWidget @@@@ Z)
谢谢
最佳答案
我认为您需要从静态函数定义中删除static
关键字:
错误:
static DatabaseEngine& DatabaseEngine::instance()
正确:
DatabaseEngine& DatabaseEngine::instance()