1

I've searched the web for this and found the similar problem a bunch but I can't quite get it fixed with my code following all that information. I'm a total beginner.

I'm getting an undefined reference to vtable for Character error. Code below.

Character.h:

#ifndef CHARACTER_H_
#define CHARACTER_H_
#include "ZorkUL.h"
#include <QObject>

#include <string>
using namespace std;
#include <vector>
using std::vector;

class Character : public QObject {    
Q_OBJECT
private:
    int health;
    int stamina;
public:
    Character();
    void setHealth(void);
    void setStamina(void);   

signals:
    void listener();    
};    
#endif /*CHARACTER_H_*/

Character.cpp:

#include "Character.h"

Character::Character() {   
    this->health = 100;
    this->stamina = 100;
}

void Character::setHealth(void){    
    this->health = health - 10;
}

void Character::setStamina(void){    
    this->stamina = stamina - 10;
}

void Character::listener(){    
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Sean
  • 31
  • 4
  • Just says "undefined reference to vtable for Character". – Sean Mar 16 '20 at 16:37
  • 2
    how do you compile all of this? Please post [MCVE] – Jean-Baptiste Yunès Mar 16 '20 at 16:38
  • 1
    This doesn't address the question, but after `using namespace std;`, `using std::vector;` doesn't add anything. – Pete Becker Mar 16 '20 at 16:39
  • Unrelated to the issue, but please see: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – AVH Mar 16 '20 at 16:40
  • Unrelated, but **never** do `using namespace std;` in the global namespace in a header file. – Ted Lyngmo Mar 16 '20 at 16:40
  • I'm sorry guys I'm a little hopeless here, it's a program I've been given and to make additions and edits to so as to sort of learn what's going on. I'm getting errors in Character.cpp and Character.h. I do know that if I leave out the listener signal altogether it all compiles fine. – Sean Mar 16 '20 at 16:41
  • This is probably a compiling error, probably `Character.cpp` is not compiled or linked at the end. Please give us the full error message! – Jean-Baptiste Yunès Mar 16 '20 at 16:42
  • Does the program use threads? – Ted Lyngmo Mar 16 '20 at 16:49
  • No it doesn't use threads so far as I'm aware. Signals and slots and Q_OBJECT. It all compiles fine so long as I'm not declaring signals in my character class. – Sean Mar 16 '20 at 16:50
  • Oh, couid [this](https://doc.qt.io/qt-5/signalsandslots.html) be it? "_The Q_OBJECT macro is expanded by the preprocessor to declare several member functions that are implemented by the moc; if you get compiler errors along the lines of "undefined reference to vtable for LcdNumber", you have probably forgotten to [run the moc](https://doc.qt.io/qt-5/moc.html) or to include the moc output in the link command._" – Ted Lyngmo Mar 16 '20 at 16:53
  • Make sure that your project file(.pro) includes Character.h file in headers section. `HEADERS += Character.h` – 273K Mar 16 '20 at 16:53
  • I mean that sounds like it's it Ted haha, I'm still as lost as ever though. It's definitely the macros that's giving me my error. – Sean Mar 16 '20 at 16:57
  • It seems like you are supposed to "_run the Meta-Object Compiler (moc)_" to avoid the problem. There's an instruction on the site too. – Ted Lyngmo Mar 16 '20 at 17:02
  • Cheers, Ted. I got it. Well, I Run->Cleaned All, the Built again. – Sean Mar 16 '20 at 17:49
  • Dunno does that count as solved haha. – Sean Mar 16 '20 at 17:49
  • @SeanBarrett I guess so - but I was just guessing so I think it's better if you write an answer to your own question. That's fine and will perhaps help others in the same situation. – Ted Lyngmo Mar 17 '20 at 11:33

1 Answers1

0

After about a month of sifting through Google to find an answer to this issue, I finally was just now able to solve it.

Most answers will tell you to delete the build directory, or run qmake again and so on, and the idea behind this (highly simplified) is that QT's Meta Object Compiler generates additional .cpp files for classes with the Q_OBJECT macro in its class declaration, and can sometimes 'forget' to generate these files when additional files that use the Q_OBJECT macro have been added to the project.

While this is true in most cases, what solved the issue for me happened after finally inspecting the mainwindow_moc.cpp file myself.

Come to find out that an external Qt Widget I imported into my project (and had since stopped using, but forgot to take out the source files from my project) had some namespace declarations and using statements that seemed to confuse the MOC when generating my mainwindow_moc.cpp file.

As a result, the MOC generated my MainWindow functions scoped under this extra widget namespace, and the generated code that looked like extra_namespace::MainWindow::staticMetaObject, instead of MainWindow::staticMetaObject.

So the solution for me was to simply remove that widget from my .pro file, and all #include directives to those header files.

So if the first solution doesn't work for you, that is, deleting the build directory and running qmake manually.. take a look at the classname_moc.cpp files generated in your build directory and make sure the generated functions necessary for the Q_OBJECT macro are scoped properly.

xdmtk
  • 31
  • 5