Forward declaration error [SOLVED]



  • 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?



  • Hi,
    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...



  • 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).



  • 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>



  • Can you post the compiler error??



  • 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;
    ^



  • 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??



  • i somehow solved it by using an additional class vertex instead of struct vertex


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.