我有一个班级:
class MakeMeshStructure : public QObject
{
Q_OBJECT
public:
MakeMeshStructure(QObject* parent = 0) {}
在其中一个函数中我这样做:
CadPanel * paneli;
int npanelov = mesh.faces_end().handle().idx();
paneli = new CadPanel[npanelov];
以后:
for(int i=0; i<npanelov;i++){
if(paneli[i].wasSet)paneli[i].draw(this);
}
这是我的小组课程:
class CadPanel : public QObject
{
Q_OBJECT
public:
CadPanel();
void draw(MakeMeshStructure* parent); //error here
错误是:错误27错误C2061:语法错误:标识符'MakeMeshStructure'
如何传递MakeMeshStructure parent以在此CadPanel函数中使用? TY
答案 0 :(得分:4)
您必须向班级添加转发类声明:
class MakeMeshStructure; // IMPORTANT: Declares a forwarding class
class CadPanel : public QObject
{
Q_OBJECT
public:
CadPanel();
void draw(MakeMeshStructure* parent); // no more error :)
}
请注意,如果类位于不同的名称空间中,则必须使用正确的名称空间限定名称。
答案 1 :(得分:1)
根据您在CadPanel :: draw()中使用MakeMeshStructure的用途,您可能需要包含包含MakeMeshStructure定义的标题:
#include "MakeMeshStructure.h"
或者只是在CadPanel的定义之前做一个前向声明:
class MakeMeshStructure;
class CadPanel : public QObject
{
...
答案 2 :(得分:1)
错误的原因是因为编译MakeMeshStructure
时编译器不知道CadPanel
类。
由于MakeMeshStructure
方法仅使用指向CadPanel
的指针,因此可以使用CadPanel
class MakeMeshStructure; // Forward declaration of MakeMeshStructure
class CadPanel
{
Q_OBJECT
public:
CadPanel();
void draw( MakeMeshStructure* parent );
};
但是,如果MakeMeshStructure
方法创建了CadPanel
的实例,则需要包含已定义CadPanel
的标头。这是因为为了创建CadPanel
的实例,需要完整的类定义。