user2366975
user2366975

Reputation: 4700

Pass a structure to a constructor

What am I doing wrong here? I need to make a second constructor that accepts a structure by value.

note.h

class Traymenu;

class Note : public QWidget
{
    Q_OBJECT
public:
    explicit Note(Traymenu *trayMenuIn, QWidget *parent = 0);  //Working
    explicit Note(Traymenu *trayMenuIn, struct prop bla, QWidget *parent = 0); //ERROR forward declaration of "struct prop"

note.h (alternativ)

class Traymenu;
struct prop;           //ERROR: forward declaration of "struct prop"

class Note : public QWidget
{
    Q_OBJECT
public:
    explicit Note(Traymenu *trayMenuIn, QWidget *parent = 0);
    explicit Note(Traymenu *trayMenuIn, struct prop bla, QWidget *parent = 0); //ERROR forward declaration of "struct prop"

note.cpp

#include "note.h"    
Note::Note(Traymenu *trayMenuIn, struct prop bla, QWidget *parent) :  //ERROR: bla has incomplete type

Upvotes: 0

Views: 562

Answers (1)

pmr
pmr

Reputation: 59811

You are accepting the structure by value, which requires its full definition to be available at that point (it is required to be a complete type). Pass by reference to avoid this:

Note(Traymenu*, const prop& bla); // reference to const is almost 
                                  // equivalent to pass by value

If the copy is really desired, include the definition of prop.

You will still need to include the definition of prop in the implementation file, if you are doing any operation on the type that requires it to be complete.

Omit the struct keyword in variable declarations, it is not required in C++ and I would consider it bad style.

Upvotes: 1

Related Questions