0

This is my main.cpp. My program starts here and this is where I have the problem:

I get two errors:

undefined reference to `BankController::BankController(TransactionRepository)* at line 23

and

undefined reference to `TransactionFileRepository::TransactionFileRepository(std::string) at line 19

For both of them, the type is C/C++ Problem, resource is main.cpp

#include "bankgui.h"
#include "Controller/BankController.h"
#include "Repository/TransactionFileRepository.h"
#include "Repository/TransactionMemoryRepository.h"
#include "Repository/TransactionRepository.h"

#include <QtGui>
#include <QApplication>
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char *argv[]){

  string path = "DataStorage/Database.txt";

  //Instantiate the main data repository
  TransactionRepository* mainDatabase;
  mainDatabase = new TransactionFileRepository(path); // <-- Error here

  //Instantiate the main controller
  BankController* mainController;
  mainController = new BankController(mainDatabase); // <-- Same Error here

  //Starts the GUI
  QApplication app(argc, argv);
  BankGUI* mainWidget;
  mainWidget = new BankGUI(mainController);
  mainWidget->show();

  return app.exec();
}

I have 3 classes:

  • a virtual TransactionRepository

  • one class that implements the above TransactionMemoryRepository

  • one class that inherits the above TransactionMemoryRepository into TransactionFileRepository

I've been searching for several hours on google for solutions but everything I try doesn't get rid of those errors.

I should mention that this is a C++ QT project that I am working on. I had to add to the project properties the include paths for some things to work.

All files that are #included exist.

Loop
  • 233
  • 1
  • 3
  • 13
  • http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – chris May 14 '13 at 17:32
  • You are not properly linking your CPP files to your header declarations. That is all this error is describing. – RandyGaul May 14 '13 at 17:32
  • reading that article now – Loop May 14 '13 at 17:53
  • possible duplicate of [Undefined reference when instantiating a class](http://stackoverflow.com/questions/16544218/undefined-reference-when-instantiating-a-class) – cmannett85 May 14 '13 at 20:57

2 Answers2

0

This is a linker error. It means that the parts (the different .cpp files) of your program compiled successfully but now the linker can't figure out how to put them together.

Just like you have to use #includes (of .h files) to tell the compiler that a function is defined in a different file you need to give parameters to the linker so he will use the compiled files (those created from the .cpp) to find those functions

The compilation process is described in the answer to this question.

Community
  • 1
  • 1
Sarien
  • 6,647
  • 6
  • 35
  • 55
  • Though I am using Eclipse IDE for C++, using the QT project workspace. I know the linking process is done automatically by the IDE. Do I have to set it up someway? – Loop May 14 '13 at 17:48
  • I don't have Eclipse set up for C++ so I can't be sure but this: http://stackoverflow.com/questions/8480013/eclipse-c-c-cdt-add-l-option-linking-math-module-gcc-lm looks good. I do find it odd that files that are in the project are not linked, tough. Maybe you made a mistake when setting things up. – Sarien May 14 '13 at 17:54
  • I don't think I know how to set it up right then. I added some library paths and that's about it. Making QT projects surely has some different mechanics under the hood. For instance, if I r-click on my Project->Properties, everything there looks different from standard cpp projects I made in the past. – Loop May 14 '13 at 18:00
  • @Loop Qt has a few other compilers/preprocessors it uses: `moc`, `uic`, and `rcc`; that's the primary toolkit difference you will see. Also I think you should ditch Eclipse, apart from being crap for C++ in general (my opinion from having to use it professionally for years), Qt Creator makes using Qt *much* easier project-wise. – cmannett85 May 14 '13 at 18:20
  • 1
    @cmannett85 Yeah, that's a problem. I am getting my grade from this project and one requirement is to use Eclipse and not QT Creator (I think because QTC has that drag-n-drop functionality that teachers stay away from) – Loop May 14 '13 at 18:34
0

Found the problem.

Apparently my .pro file that handles the QT linkage process was not up to date. I had missing .header files in the HEADERS/SOURCES attributes inside. Because of that, they didn't use them.

Loop
  • 233
  • 1
  • 3
  • 13