Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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(){

}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top