这是我的代码:
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&gt; const&amp;,class std :: basic_string ,类std :: allocator&gt; const&amp;)“(?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:static class 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个未解析的外部
我在这里做错了什么?我检查了几次,ContactsController.h
和ContactsController.cpp
中的函数签名是相同的。这里应该没有链接错误。