提问人:bobodrg 提问时间:6/6/2023 最后编辑:Benjamin Buchbobodrg 更新时间:6/11/2023 访问量:221
如何修复“LNK2019未解析的外部符号”错误?
How to fix "LNK2019 unresolved external symbol" error?
问:
我需要在 Visual Studio 2022 的项目中使用 C++ 和 Qt VS 工具。
如果我尝试使用 ,我应该包括一些特定的库,如 、 、 ,但是在初始化本地 Windows 调试器后,我遇到了这种错误:QtSql
<QtSql/QSqlDatabase>
<QtSql/QSqlError>
<QtSql/QSqlQuery>
Severity : Error
Code : LNK2019
Description : unresolved external symbol "__declspec(dllimport) public: __cdecl QSqlDatabase::QSqlDatabase(void)" (_`_`imp`_`??0QSqlDatabase@@QEAA@XZ) referenced in function "private: void __cdecl LoginPage::onLoginButtonClicked(void)" (?onLoginButtonClicked@LoginPage@@AEAAXXZ)
Project : StockManager
File : D:\POO-P\StockManager\StockManager\LoginPage.obj
Line : 1
Suppression State :
头文件:
#pragma once
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include "ui_LoginPage.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
class LoginPage : public QWidget
{
Q_OBJECT
public:
explicit LoginPage(QWidget* parent = nullptr);
QPushButton* getLoginButton() const { return loginButton; }
QPushButton* getRegisterButton() const { return registerButton; }
bool createConnection();
~LoginPage();
private slots:
void onLoginButtonClicked();
void onRegisterButtonClicked();
signals:
void authenticationSuccessful();
private:
QPushButton* loginButton;
QPushButton* registerButton;
QLineEdit* usernameLineEdit;
QLineEdit* passwdLineEdit;
Ui::LoginPageClass* ui;
};
源文件:
#include "LoginPage.h"
LoginPage::LoginPage(QWidget* parent) : QWidget(parent), ui(new Ui::LoginPageClass)
{
ui->setupUi(this);
ui->passwdLineEdit->setEchoMode(QLineEdit::Password);
loginButton = ui->loginButton;
registerButton = ui->registerButton;
createConnection();
connect(ui->loginButton, &QPushButton::clicked, this, &LoginPage::onLoginButtonClicked);
connect(ui->registerButton, &QPushButton::clicked, this, &LoginPage::onRegisterButtonClicked);
}
bool LoginPage::createConnection() {
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("BOGDAN");
db.setDatabaseName("Driver={SQL Server Native Client 11.0};Server=BOGDAN;Database=StockManager;Trusted_Connection=yes;");
if (!db.open()) {
qDebug() << "Connection failed" << db.lastError().text();
return false;
}
else {
qDebug() << "Successfully connected!";
return true;
}
}
void LoginPage::onLoginButtonClicked()
{
QString username = ui->usernameLineEdit->text();
QString password = ui->passwdLineEdit->text();
QSqlQuery query;
query.prepare("SELECT * FROM Users WHERE Username = :username AND Password = :password");
query.bindValue(":username", username);
query.bindValue(":password", password);
if (query.exec() && query.next()) {
emit authenticationSuccessful();
}
else {
// Invalid login
}
}
void LoginPage::onRegisterButtonClicked()
{
// ... codul tău aici
}
LoginPage::~LoginPage() {
// nu mai este necesar să ștergi ui manual
}
我知道这是一个链接器问题,但我试图去 ,并添加 和 ,但它不起作用,这是我得到的:Project Properties -> Linker -> Input -> Add dependencies
Qt5Sql.lib
Qt5Sqld.lib
Code : LNK1104
Description : cannot open file 'Qt5Sqld.lib'
Project : StockManager
File : D:\POO-P\StockManager\StockManager\LINK
Line : 1
答: 暂无答案
评论
QT += sql
qmake