1

I'm having so much trouble with multiple definitions in QT. For ordinary classes, and in my entire library, I just put the implementation and header in the same hpp file. I'm triying to convert an ordinary class in a QObject, so i can use this as a worker to connect with other QThread. After I converted my class in a QObject, I have experienced many multiple definition issues. Suppose that my class now looks like this:

#ifndef MYCLASS_HPP
#define MYCLASS_HPP
#include "common.hpp"
#include <qtGui>
namespace Bial
{
class Image;

class Myclass : QObject{
    Image *img;
signal:
    void mySignal();
public:
    void f();
}

#include "Image.hpp"
namespace Bial{
void Myclass::f(){

}
}
#endif //MYCLASS_HPP

MyClass is a simplification of the Platefinder class. Thai is too big to put here; The issue occurs in the moc_platefinder.o file of this class ans in many functions of my entire library. Totaling 289 multiple definitions issues :

mainwindow.o:/home/lellis/Dropbox/Lellis_Diet/bin/../diet/inc/../bial/File.hpp:1677: first defined here

Entire compiler output

Sorry for my bad english.

Lucas Lellis
  • 43
  • 1
  • 5

2 Answers2

1

Shouldn't you include 'Q_OBJECT'(http://qt-project.org/doc/qt-4.8/signalsandslots.html) here? :

class Myclass : QObject{

Q_OBJECT // <-- here

signal:
    void mySignal();
public:
    void f();
    Image *img;

}

Edit: Generally 'multiple definitions of ...' can be repared with 'static' (ie 'static void f()')

Edit: Did you look here? Multiple definitions error: one in my file and one in the moc file.

Community
  • 1
  • 1
us3r
  • 48
  • 8
  • Sorry, I tried to simplify the class to make the question, but forgot to include the Q_OBJECT tag. The multiple definitiion issue occurs with many member classes of this class named PlateFinder, but it occurs in many other classes. I have 289 multiple definition issues like this :/ – Lucas Lellis Apr 20 '14 at 19:13
  • I had the same problem once and I think I had to recompile the project (not sure about that). Edit: Look at the edit – us3r Apr 20 '14 at 20:30
  • My problem is different, i know that this signals are implemented in moc file. I'm triyng to find a way to compile this program without make this 289 functions inline.. Unfortunately i will have to do this for now.. Tanks for the help :) – Lucas Lellis Apr 20 '14 at 21:11
0

I haven't tried my answer but:

  1. #include QObject make no sense; you could use #include instead;
  2. In a class derived by QObject you have to add a Q_OBJECT macro in his definition;
  3. #endif appears in a wrong position;

Then, your code should look like this:

#ifndef MYCLASS_HPP
#define MYCLASS_HPP
#include "common.hpp"
#include <QtGui>

namespace Bial
{
class Image;

class Myclass : QObject{
    Q_OBJECT

    Image *img;
signal:
    void mySignal();
public:
    void f();
}

#endif //MYCLASS_HPP

#include "Image.hpp"
namespace Bial{
void Myclass::f(){

}
}
Salvatore Avanzo
  • 2,656
  • 1
  • 21
  • 30
  • I tried to do this now, but didn't work. I tried to simplify my class to make the question, but forgot to add the Q_OBJECT tag in MyClass header. The #endif tag is in the end beacuse the header and implementation are in the same file, and it works fine in all other classes. Even changing the #endif tag position ans including qtGUI i'm having the same issue. After moc created the moc_platefinder.cpp file, the QT compiler showed 289 multiple definitions issue. – Lucas Lellis Apr 20 '14 at 19:24