将结构传递给构造函数
问题描述:
我在这里做错了什么? 我需要做第二个构造函数接受一个值的结构。将结构传递给构造函数
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(可选择性)
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
答
您正在接受由值的结构中,要求其完整定义在该点可用(它需要是complete type
)。通过参考以避免此:
Note(Traymenu*, const prop& bla); // reference to const is almost
// equivalent to pass by value
如果真的需要复制,请包括prop
的定义。
如果您对需要完成的类型进行任何操作,您仍然需要在实现文件中包含prop
的定义。
在变量声明中省略了struct
关键字,它在C++中不是必需的,我会认为它是不好的样式。
'struct'关键字可以用来转发声明 – Paranaix
@Paranaix是的,这是必要的,但不是在接受参数时。 – pmr
我在另一个调用'Note'构造函数的类中声明了我的'struct prop'作为公共成员变量。嗯。它不能被引用,因为在调用构造函数 – user2366975