将结构传递给构造函数

时间:2014-02-25 17:22:30

标签: c++ qt

我在这里做错了什么? 我需要创建一个按值接受结构的第二个构造函数。

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

1 个答案:

答案 0 :(得分:1)

您正在按值接受结构,这要求其完整定义在此时可用(它必须是complete type)。通过引用传递以避免这种情况:

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

如果确实需要副本,请包含prop的定义。

如果您要对要求完成的类型进行任何操作,您仍需要在实现文件中包含prop的定义。

在变量声明中省略struct关键字,在C ++中不需要它,我会认为它样式不好。