Forward declaration error [SOLVED]
-
wrote on 16 Apr 2015, 10:04 last edited by droelf
I have a struct called vertex and it needs a pointer variable to the mainWindow class.
But it gives me an error either with forward declaration or without.class MainWindow;
class disk;
class sceneMouselistener;struct vertex{
typedef pair<float,vertex*> ve; vector<ve> adjVec; //cost of edge, destination vertex int name; int type; bool matched; QPointF pos; disk *vDisk; sceneMouselistener *painter; MainWindow *mainW; float yv; vertex(int nr,QPointF initPos,MainWindow *w,int t){ mainW = w; name = nr; pos = initPos; yv = 0; matched = false; type = t; float tempRadius = 0.0; vDisk = new disk(pos,tempRadius,mainW); painter = mainW->scene; if(type==1||type==2){ //painter->addEllipse(pos.x()-10,pos.y()-10,vDisk.radius,vDisk.radius,); painter->addEllipse(pos.x()-10,pos.y()-10,10,10,mainW->blackPen,mainW->blackBrush); }else if(type==3){ //painter->addEllipse(pos.x()-10,pos.y()-10,vDisk.radius,vDisk.radius,); painter->addEllipse(pos.x()-10,pos.y()-10,10,10,mainW->redPen,mainW->redBrush); } } QPointF getPos(){ return pos; } int getName(){ return name; } void setPos(QPointF p){ pos = p; } float getPot(){ return yv; } void setPot(float newPot){ yv = newPot; } void update(int r){ vDisk.radius = r; if(type==1||type==2){ //painter->addEllipse(pos.x()-10,pos.y()-10,vDisk.radius,vDisk.radius,); painter->addEllipse(pos.x()-10,pos.y()-10,10,10,mainW->blackPen,mainW->blackBrush); }else if(type==3){ //painter->addEllipse(pos.x()-10,pos.y()-10,vDisk.radius,vDisk.radius,); painter->addEllipse(pos.x()-10,pos.y()-10,10,10,mainW->redPen,mainW->redBrush); } }
};
So how do i get access to the mainWindow properly?
-
I have a struct called vertex and it needs a pointer variable to the mainWindow class.
But it gives me an error either with forward declaration or without.class MainWindow;
class disk;
class sceneMouselistener;struct vertex{
typedef pair<float,vertex*> ve; vector<ve> adjVec; //cost of edge, destination vertex int name; int type; bool matched; QPointF pos; disk *vDisk; sceneMouselistener *painter; MainWindow *mainW; float yv; vertex(int nr,QPointF initPos,MainWindow *w,int t){ mainW = w; name = nr; pos = initPos; yv = 0; matched = false; type = t; float tempRadius = 0.0; vDisk = new disk(pos,tempRadius,mainW); painter = mainW->scene; if(type==1||type==2){ //painter->addEllipse(pos.x()-10,pos.y()-10,vDisk.radius,vDisk.radius,); painter->addEllipse(pos.x()-10,pos.y()-10,10,10,mainW->blackPen,mainW->blackBrush); }else if(type==3){ //painter->addEllipse(pos.x()-10,pos.y()-10,vDisk.radius,vDisk.radius,); painter->addEllipse(pos.x()-10,pos.y()-10,10,10,mainW->redPen,mainW->redBrush); } } QPointF getPos(){ return pos; } int getName(){ return name; } void setPos(QPointF p){ pos = p; } float getPot(){ return yv; } void setPot(float newPot){ yv = newPot; } void update(int r){ vDisk.radius = r; if(type==1||type==2){ //painter->addEllipse(pos.x()-10,pos.y()-10,vDisk.radius,vDisk.radius,); painter->addEllipse(pos.x()-10,pos.y()-10,10,10,mainW->blackPen,mainW->blackBrush); }else if(type==3){ //painter->addEllipse(pos.x()-10,pos.y()-10,vDisk.radius,vDisk.radius,); painter->addEllipse(pos.x()-10,pos.y()-10,10,10,mainW->redPen,mainW->redBrush); } }
};
So how do i get access to the mainWindow properly?
wrote on 16 Apr 2015, 10:16 last edited byHi,
posting the actual compiler error would be helpful.But I guess that you get the first error at
painter = mainW->scene;
A forward declaration works if you just want to declare a pointer variable of this type, but to access members of this class, the complete class definition has to be known.So you can either move the implementation to a *.cpp file where you include the header of MainWindow, or replace the forward declaration with this include...
-
wrote on 16 Apr 2015, 10:21 last edited by
Hi,
as a general C++ rule: if you need to use an object (not only declaring it) you need to include the header file (forward declaration in not enough).
-
wrote on 16 Apr 2015, 10:32 last edited by
sry forgot to post my includings:
#ifndef GRAPH_H
#define GRAPH_H
#include <iostream>
#include <vector>
#include <QVector>
#include <map>
#include <string>
#include <QtGlobal>
#include <QPoint>
#include <mainwindow.h>
#include <scenemouselistener.h>
#include <disk.h> -
wrote on 16 Apr 2015, 10:35 last edited by
Can you post the compiler error??
-
wrote on 16 Apr 2015, 10:44 last edited by
Sure sry:
Error: invalid use of incomplete type 'class MainWindow'
this->painter = mainWin->scene;
^C:\Users\Nutzer\Documents\BlossomShrinkingVisual\scenemouselistener.h:11: Error: forward declaration of 'class MainWindow'
class MainWindow;
^ -
wrote on 16 Apr 2015, 10:48 last edited by
this->painter = mainWin->scene;
Usually means there's a missing #include
scenemouselistener.h:11: Error: forward declaration of 'class MainWindow'
can you post that file??
-
wrote on 16 Apr 2015, 11:11 last edited by
i somehow solved it by using an additional class vertex instead of struct vertex
1/8