Mi Hu
Mi Hu

Reputation: 11

Constructor cannot be redeclared. Why?

I have this problem in my Qt C++ program and it says "Constructor cannot be redeclared." How can I fix it?

class MagicTransaction : public QObject
{
  Q_OBJECT
public:
  explicit MagicTransaction ( QObject* parent = 0 );
private slots:
  void start ( );
  MagicTransaction( QObject* parent );
};

Upvotes: 0

Views: 2137

Answers (4)

Mureinik
Mureinik

Reputation: 312219

You have two declarations of the MagicTransaction constructor that takes a QObject* argument - one in the public section and one in the private section (note that the default value and the explicit modifier don't take part in the name mangling). Just remove one and you should be fine:

class MagicTransaction : public QObject
{
  Q_OBJECT
public:
  explicit MagicTransaction ( QObject* parent = 0 );
private slots:
  void start ( );
  // second definition removed here
};

Upvotes: 2

rbaleksandar
rbaleksandar

Reputation: 9701

You have the constructor

explicit MagicTransaction ( QObject* parent = 0 );

and then you have a function that is

MagicTransaction( QObject* parent );

Slots are functions hence you have two identical functions one of which is a constructor and the other - not. Rename the second and you will be good to go.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882566

I have never seen a constructor used as a slot in Qt, that seems bizarre.

Slots are called only after an object is fully constructed, and I'm pretty certain C++ disallows constructing the same object more than once.

If you want a slot that will somehow accept a parent pointer (I'm not sure if you're trying to change ownership of the object in the Qt hierarchy, that also seems unusual but I think it's possible), you'll need to call it something else, like:

void ChangeMyOwner(QObject *parent);

Upvotes: 1

code_fodder
code_fodder

Reputation: 16401

Quite simply - you have declared the same constructor twice and in different sections:

class MagicTransaction : public QObject
{
  Q_OBJECT
public:
  explicit MagicTransaction ( QObject* parent = 0 );
private slots:
  void start ( );
  MagicTransaction( QObject* parent ); // <---- remove this
};

Remove the second declaration

Upvotes: 1

Related Questions