Multiple Definitions in the moc. file of a class

Asked

Viewed 77 times

0

I’m having many 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. In order to use the slots and Signals, I transformed this class, which I used to compile normally, into a Qobject. Staying, 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 from Multiple Definitions (289 in total). Remember that Myclass is a short version of the Platefindercontroller class. The errors look a lot like this one below, but the compiler output, with the 289 errors can be seen in : http://pastebin.com/L9cx7hv9

Platefindercontroller. o: In Function Bial::PlatefinderController::NextGroup()': /home/lellis/Dropbox/Lellis_Diet/bin/../diet/inc/PlatefinderController.hpp:103: multiple definition ofBial::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 prevent this?

1 answer

2

Putting the statement and setting in the header is not a very good idea.

Each build unit (e.g., .cpp) that includes this header will get a copy of the definition and so you will get these linkage errors unless you force these methods to be inline, but it is not always possible.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.