1

I'm a beginner at programming, know my way around with Java but I'm currently having quite some trouble with C++. I'm getting the following error: Unresolved External Symbol, when I try to do this in a function of a different class:

player1 = new Character(50, 300, linep1);

Error:

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Character::Character(int,int,class QLine *)" (??0Character@@QEAA@HHPEAVQLine@@@Z) referenced in function "public: void __cdecl MainWindow::setUpPlayers(void)" (?setUpPlayers@MainWindow@@QEAAXXZ)

My Character header file looks like this:

#include <QLine>

class Character
{
public:
Character(int xCoor, int yCoor, QLine *line);

QLine getView();
int getX();
int getY();
int getScore();

private:
QLine *view;
const int x;
int y;
int score;

};

The upper part of my cpp file looks like this:

#include "character.h"

Character::Character(int xCoor, int yCoor, QLine *line)
{
    score = 0;
    x = xCoor;
    y = yCoor;
    view = line;
}

As you can see I'm currently working with Qt as well. Could anyone help me out with this? Much appreciated!

László Papp
  • 51,870
  • 39
  • 111
  • 135
Robin
  • 143
  • 12
  • For the record, you *really* should avoid including headers in headers. Just use forward-declaration in the header if you need to. I am referring to the `QLine` class. Just above `Class Character` you can type `Class QLine;` then move the include to the cpp file. – Cory Kramer Apr 08 '14 at 16:36
  • Can you edit your post to include the exact error text? – Cory Kramer Apr 08 '14 at 16:37
  • Just included the complete error! – Robin Apr 08 '14 at 16:40
  • @Cyber: it is fine to include QLine. It will not change between versions, etc, so it is not much of overhead. I would personally rather remove the pointer to make the arithmetic a bit ismpler. – László Papp Apr 08 '14 at 19:15

3 Answers3

0

Unresolved external symbol refers to something outside the project, so a reference to something like an external library. First, double check to make sure all the project can find the associated files (QLine). Usually that is the cause of that error. Then I'd declare the object as such character player1(50, 300, line1);

Dortimer
  • 619
  • 8
  • 25
  • How can I check if the project can find the associated files? I just tried removing everything that had to do with QLine but it still gave me the same error, now only showing Character::Character(int,int) – Robin Apr 08 '14 at 16:50
  • Usually this is just something simple like putting the .dll in the project directory, and changing the linker and dependencies properties. In this case it seems like it is a reference to your Character class, in which case I would double check the include statements and object initialization. – Dortimer Apr 08 '14 at 20:43
0

Creating executable programs is a two-stage process in c/c++. The first stage involves compiling every source file (cpp) into object code. The second stage involves linking all the object files together, along with any other libraries, into the final executable file.

Whenever you see an "unresolved symbol" error, it means that you've successfully completed the compilation stage and all of your source files have correctly compiled. The error arises because the linker has been through all your source files, as well as any extra libraries you've mentioned, and it can't find some piece of code, or a variable, that you've used in another file somewhere.

Now, the error says that it can't find the code for the constructor of your class Character. Since it's a class you've written yourself, I suggest that your project file is not complete.

Check that the character.cpp file is included in the SOURCES list and then run qmake again.

RobbieE
  • 4,280
  • 3
  • 22
  • 36
0

You are most likely lacking this in your qmake project file:

SOURCES += character.cpp

If you are using QtCreator, make sure qmake is rerun. Otherwise just rerun it from the command line.

László Papp
  • 51,870
  • 39
  • 111
  • 135