0

Here is my code:

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;
}

Here are the errors I am getting:

Mainwindow.obj:-1: error: LNK2019: unresolved external symbol "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) referenced in function "private: void __cdecl MainWindow::on_addNewContactButton_clicked(void)" (?on_addNewContactButton_clicked@MainWindow@@AEAAXXZ)

Mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: static class QStringList const __cdecl ContactsController::prepareContacts(void)" (?prepareContacts@ContactsController@@SA?BVQStringList@@XZ) referenced in function "private: void __cdecl MainWindow::updateContactsList(void)" (?updateContactsList@MainWindow@@AEAAXXZ)

debug\Trofimov01.exe:-1: error: LNK1120: 2 unresolved externals

What am I doing wrong here? I checked a few times that the function signature is the same in both ContactsController.h and ContactsController.cpp. There should be no linking errors here.

oobarbazanoo
  • 397
  • 1
  • 5
  • 15
  • 1
    It looks like **ContactsController.cpp** isn't being compiled / linked – Justin May 22 '18 at 18:55
  • Unrelated: With a destructor and no copy constructor and assignment operator shown [you may be setting yourself up for a Rule of Three Violation.](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – user4581301 May 22 '18 at 19:01
  • @user4581301 If the type inherits from `QObject` and doesn't explicitly define copy/move operations (which `MainWindow` does, since it inherits from `QMainWindow`), that's not a problem, as it will be uncopyable and immovable – Justin May 22 '18 at 19:12
  • Ah. Good to know. – user4581301 May 22 '18 at 19:13
  • The implementation should not have the keyword `static`. – tuple_cat May 22 '18 at 20:36
  • Check the .pro ContactsController.cpp didn't add or mis-spelling. – JustWe May 24 '18 at 02:54

0 Answers0