Multiple Definitions in the moc.o file of a class

0

I'm having a lot of multiple definitions problems in QT. For common classes, and all classes in our library, the solution was to put the header and implementation in the same .hpp, and it worked. To be able to use the slots and signals, I made this class, which before compiled normally, into a QObject. Briefly, with this format:

#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();
}
//Implementação;
#include "Image.hpp"
namespace Bial{
void Myclass::f(){

}

}

#endif //MYCLASS_HPP

However, when compiling this program I get many errors of multiple definitions (289 in total). Remembering that MYClass is a shortened version of the PlatefinderController class. The errors look a lot like this below, but the compiler output with the 289 errors can be seen at: link

PlatefinderController.o: In function Bial::PlatefinderController::NextGroup()': /home/lellis/Dropbox/Lellis_Diet/bin/../diet/inc/PlatefinderController.hpp:103: multiple definition of Bial :: PlatefinderController :: NextGroup () '     mainwindow.o: / home / lellis / Dropbox / Lellis_Diet / bin /../ diet / inc / PlatefinderController.hpp: 103: first defined here

Is there anything I can do to avoid this?

    
asked by anonymous 20.04.2014 / 20:12

1 answer

2

Placing the declaration and definition in the header is not a very good idea.

Each compile drive (eg .cpp) that includes this header will have a copy of the definition and so you will get these linking errors, unless you force those methods to be inline, but not always this is possible.

In the case of Qt classes this is less recommended because many things will still be done by moc and new methods will be added to your class.

Move the definition of your methods to your own .cpp file, if doing so you are having linking errors, better seek to understand why.

    
08.05.2014 / 19:51