c++ - QT中的LNK2019错误
问题描述
这是我的代码:
Mainwindow.cpp
:
#include "Mainwindow.h"
#include "ui_Mainwindow.h"
#include "ContactsController.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
updateContactsList();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_addNewContactButton_clicked()
{
ContactsController::updateContacts(
ui->lineName->text().toStdString(),
ui->lineNumbers->text().toStdString()
);
updateContactsList();
/*std::string name(ui->lineName->text().toUtf8().constData());
std::string numbers(ui->lineNumbers->text().toUtf8().constData());
// ContactsManager
std::vector<std::string> numbersVector(ContactsManager::parseNumbers(numbers));
Contact newContact = {
name,
numbersVector
};
ContactsManager::addContact(newContact);
updateContactsList();*/
}
void MainWindow::updateContactsList()
{
ui->contactsList->clear();
const QStringList itemsList(ContactsController::prepareContacts());
ui->contactsList->addItems(itemsList);
}
ContactsController.h
:
#ifndef CONTACTSCONTROLLER_H
#define CONTACTSCONTROLLER_H
#include <string>
#include <QStringList>
#include "ContactsModel.h"
class ContactsController
{
std::vector<std::string> parseNumbers(std::string numbers);
static std::string contactRepresentation(const Contact&);
public:
/*static std::vector<Contact> availableContacts;
static std::vector<Contact> readContacts();
static void updateContacts();
static std::vector<std::string> parseNumbers(std::string);
static void addContact(const Contact&);*/
static void updateContacts(const std::string&, const std::string&);
static const QStringList prepareContacts();
};
#endif // CONTACTSCONTROLLER_H
ContactsController.cpp
:
#include "ContactsController.h"
static void ContactsController::updateContacts(const std::string& name, const std::string& numbers) {
Contact contact = {name, ContactsController::parseNumbers(numbers)};
ContactsModel::upsert(contact);
}
std::vector<std::string> ContactsController::parseNumbers(std::string numbers)
{
std::vector<std::string> res;
const std::string delimiter(",");
size_t pos = 0;
std::string token;
while ((pos = numbers.find(delimiter)) != std::string::npos) {
token = numbers.substr(0, pos);
res.push_back(token);
numbers.erase(0, pos + delimiter.length());
}
return res;
}
const QStringList ContactsController::prepareContacts() {
QStringList res;
map<std::string, Contact> allContacts(ContactsModel::allContacts());
map<std::string, Contact>::iterator it(allContacts.begin());
while (it != allContacts.end()) {
QString qstring(QString::fromUtf8(contactRepresentation(it->second)));
res.append(qstring);
++it;
}
return res;
}
static std::string contactRepresentation(const Contact& contact) {
std::res(contact.name + " ");
const std::vector<std::string>& numbers(contact.numbers);
bool previousExist(false);
for(std::vector<T>::iterator it(numbers.begin()); it != numbers.end(); ++it) {
/* std::cout << *it; ... */
if(previousExist) {
res+=";";
}
res+=(*it);
previousExist = true;
}
return res;
}
以下是我得到的错误:
Mainwindow.obj:-1: 错误: LNK2019: 无法解析的外部符号“public: static void __cdecl ContactsController::updateContacts(class std::basic_string,class std::allocator > const &,class std::basic_string,class std:: allocator > const &)" (?updateContacts@ContactsController@@SAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) 在函数 "private : void __cdecl MainWindow::on_addNewContactButton_clicked(void)" (?on_addNewContactButton_clicked@MainWindow@@AEAAXXZ)
Mainwindow.obj:-1:错误:LNK2019:未解析的外部符号“public:静态类 QStringList const __cdecl ContactsController::prepareContacts(void)”(?prepareContacts@ContactsController@@SA?BVQStringList@@XZ) 在函数“private: void __cdecl MainWindow::updateContactsList(void)" (?updateContactsList@MainWindow@@AEAAXXZ)
debug\Trofimov01.exe:-1: 错误: LNK1120: 2 unresolved externals
我在这里做错了什么?我检查了几次,函数签名在ContactsController.h
和中是相同的ContactsController.cpp
。这里应该没有链接错误。
解决方案
推荐阅读
- javascript - 在数据表上添加条件多列过滤器
- pyspark - Pyspark ..滞后于生成/计算的列
- rest - 子 api 的包装器 api (ASP.NET Core2)
- c# - 将怪物变量传递给战斗控制器
- forms - 我想创建一个在多页中使用的页眉和页脚。我可以编写一次代码并在 xamarin 表单的多个页面上调用它吗
- android - Scrollview 中的 Viewpager 不起作用
- c# - 打开一个 Windows 资源管理器进程,然后再次终止它
- aframe - 使用联网 A-Frame (NAF) 组件时,如何禁用 LERPing?
- android - 当用户不活动时如何使用Android中所有活动的通用方法注销登录活动
- amazon-web-services - 使用 Athena 从云端日志中检测位置、浏览器和设备类型