我在这里做错了什么? 我需要制作第二个按值接受结构的构造函数。
注意.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"
笔记.cpp
#include "note.h"
Note::Note(Traymenu *trayMenuIn, struct prop bla, QWidget *parent) : //ERROR: bla has incomplete type
最佳答案
您正在按值接受结构,这要求它的完整定义在此时可用(它必须是完整类型
)。通过引用传递以避免这种情况:
Note(Traymenu*, const prop& bla); // reference to const is almost
// equivalent to pass by value
如果确实需要复制,请包括 prop
的定义。
如果您正在对需要它完成的类型执行任何操作,您仍然需要在实现文件中包含 prop
的定义。
在变量声明中省略 struct
关键字,它在 C++ 中不是必需的,我认为它是糟糕的风格。
关于c++ - 将结构传递给构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22021583/