Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Forward declaration error [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Forward declaration error [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 3.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    droelf
    wrote on last edited by droelf
    #1

    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?

    M 1 Reply Last reply
    0
    • D 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?

      M Offline
      M Offline
      Mario84
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcosta
        wrote on last edited by
        #3

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

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply
        0
        • D Offline
          D Offline
          droelf
          wrote on last edited by
          #4

          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>

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            Can you post the compiler error??

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply
            0
            • D Offline
              D Offline
              droelf
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mcosta
                wrote on last edited by
                #7

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

                Once your problem is solved don't forget to:

                • Mark the thread as SOLVED using the Topic Tool menu
                • Vote up the answer(s) that helped you to solve the issue

                You can embed images using (http://imgur.com/) or (http://postimage.org/)

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  droelf
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved