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 of Class and Incomplete Type
QtWS25 Last Chance

Forward Declaration of Class and Incomplete Type

Scheduled Pinned Locked Moved Solved General and Desktop
incomplete typeforwarddeclarationclass
7 Posts 3 Posters 19.8k Views
  • 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.
  • R Offline
    R Offline
    RBrNx277
    wrote on last edited by RBrNx277
    #1

    Hi there, I am currently having a problem with circular dependencies. I have a SokoGenerator class which holds a struct like so

    #ifndef SOKOGENERATOR_H
    #define SOKOGENERATOR_H
    
    #include <QObject>
    #include <QThread>
    #include <QString>
    #include <vector>
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <time.h>
    #include <queue>
    #include <tuple>
    #include <chrono>
    #include <random>
    #include "solvercpp/solver.h"
    #include "difficultyanalyser.h"
    
    
    
    class SokoGenerator : public QObject{
        Q_OBJECT
        typedef vector<vector<char>> TwoDVector_char;
        typedef vector<vector<int>> TwoDVector_int;
        typedef std::chrono::steady_clock::time_point time;
        typedef std::chrono::duration<int, std::milli> millisecs_t;
        typedef unsigned long long ull;
    
    
    public:
        struct Level {
            TwoDVector_char grid;
            string solution;
            int generationTime;
            QString difficulty;
        };
    
        explicit SokoGenerator(QObject *parent = 0);
        ~SokoGenerator();
    
        Solver solver;
        DifficultyAnalyser diffAnalyser;
        std::default_random_engine generator;
        std::uniform_int_distribution<int> distribution;
    
        //Some functions
    
    };
    
    #endif // SOKOGENERATOR_H
    

    The difficultyAnalyser Class is then declared with the following:

    #ifndef DIFFICULTYANALYSER_H
    #define DIFFICULTYANALYSER_H
    
    #include <QObject>
    #include <string>
    #include "sokogenerator.h"
    
    using namespace std;
    
    
    class DifficultyAnalyser : public QObject
    {
        Q_OBJECT
    public:
        explicit DifficultyAnalyser(QObject *parent = 0);
        QString calculateDifficulty(SokoGenerator::Level level);
    
    private:
        int calculatePushes(string solution);
        int calculateLines(string solution);
        int calculateBoxes(SokoGenerator::Level level);
        int neighbourCheck(SokoGenerator::Level level, int x, int y, char first, char second = NULL);
        int furtherCalculations(SokoGenerator::Level level);
    
    
    signals:
    
    public slots:
    };
    
    #endif // DIFFICULTYANALYSER_H
    

    So I'm needing a few of the DifficultyAnalyser functions to take an instance of the struct in SokoGenerator as a parameter. However I get the error

    SokoGenerator has not been declared

    I then read a bit about circular dependencies and tried to use forward declaration in the SokoGenerator header using

    class DifficultyAnalyser;
    

    And also

    QT_FORWARD_DECLARE_CLASS(DifficultyAnalyser);
    

    This code by itself will run no problem, however if I then add

    DifficultyAnalyser* diffAnalyser;
    diffAnalyser->calculateDifficulty(newLevel);
    

    to a function inside the SokoGenerator class I get the error:

    invalid use of incomplete type 'class DifficultyAnalyser'
    forward declaration of 'class DifficultyAnalyser'

    Is there something I am missing?

    Thanks, Conor

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You have a circular dependency. You should forward declare DifficultyAnalyser in SokoGenerator and use a pointer to it.

      However, are you sure you need the Level struct to be part of SokoGenerator ? If you take it out properly you can avoid the circular dependency.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You have a circular dependency. You should forward declare DifficultyAnalyser in SokoGenerator and use a pointer to it.

        However, are you sure you need the Level struct to be part of SokoGenerator ? If you take it out properly you can avoid the circular dependency.

        R Offline
        R Offline
        RBrNx277
        wrote on last edited by
        #3

        @SGaist Hi there, thanks for the reply. I tried using a forward declaration in the SokoGenerator class with

        //Includes here
        
        QT_FORWARD_DECLARE_CLASS(DifficultyAnalyser)
        
        class SokoGenerator : public QObject{
            Q_OBJECT
            typedef vector<vector<char>> TwoDVector_char;
            typedef vector<vector<int>> TwoDVector_int;
            typedef std::chrono::steady_clock::time_point time;
            typedef std::chrono::duration<int, std::milli> millisecs_t;
            typedef unsigned long long ull;
        
        
        public:
            struct Level {
                TwoDVector_char grid;
                string solution;
                int generationTime;
                QString difficulty;
            };
        
            explicit SokoGenerator(QObject *parent = 0);
            ~SokoGenerator();
        
            Solver solver;
            DifficultyAnalyser* diffAnalyser;
            
        public:
            //Some functions
        
        

        Which seems to compile fine. However once I actually try to use the DifficultyAnalyser object like so

        newLevel.difficulty = diffAnalyser->calculateDifficulty(newLevel);
        

        it will give me the error
        invalid use of incomplete type 'DifficultyAnalyser'

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You still need to include the header where you use the class.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          0
          • SGaistS SGaist

            You still need to include the header where you use the class.

            R Offline
            R Offline
            RBrNx277
            wrote on last edited by RBrNx277
            #5

            @SGaist OK I have included the DifficultyAnalyser header in the SokoGenerator class, but now I get the error

            SokoGenerator has not been declared
            

            on all of the lines where I use

            SokoGenerator::Level
            

            inside the DifficultyAnalyser class

            #ifndef DIFFICULTYANALYSER_H
            #define DIFFICULTYANALYSER_H
            
            #include <QObject>
            #include <string>
            #include "sokogenerator.h"
            
            using namespace std;
            
            class DifficultyAnalyser : public QObject
            {
                Q_OBJECT
            public:
                explicit DifficultyAnalyser(QObject *parent = 0);
                QString calculateDifficulty(SokoGenerator::Level level);
            
            private:
                int calculatePushes(string solution);
                int calculateLines(string solution);
                int calculateBoxes(SokoGenerator::Level level);
                int neighbourCheck(SokoGenerator::Level level, int x, int y, char first, char second = NULL);
                int furtherCalculations(SokoGenerator::Level level);
            
            
            signals:
            
            public slots:
            };
            
            #endif // DIFFICULTYANALYSER_H
            
            
            1 Reply Last reply
            0
            • A Offline
              A Offline
              alex_malyu
              wrote on last edited by alex_malyu
              #6

              idea is simple:
              forward declaration allows to declare pointer to incomplete class,
              but as soon you need access functionality of incomplete class you have to provide complete declaration (include file).
              This usually works well if you do not define classes in the same file and keep class implementation in cpp file

              // b.h header file
              // forward declaration
              class A;

              class B {

              public:
              A* pA; // pointer of A is fine

              }

              // B. cpp file

              #include b.h
              #include a.h // Complete declaration of a

              B b;
              b.pA = new A;

              R 1 Reply Last reply
              0
              • A alex_malyu

                idea is simple:
                forward declaration allows to declare pointer to incomplete class,
                but as soon you need access functionality of incomplete class you have to provide complete declaration (include file).
                This usually works well if you do not define classes in the same file and keep class implementation in cpp file

                // b.h header file
                // forward declaration
                class A;

                class B {

                public:
                A* pA; // pointer of A is fine

                }

                // B. cpp file

                #include b.h
                #include a.h // Complete declaration of a

                B b;
                b.pA = new A;

                R Offline
                R Offline
                RBrNx277
                wrote on last edited by
                #7

                @alex_malyu Thanks very much that's what I was missing.
                To get it working I removed the include for DifficultyAnalyser in the SokoGenerator header file
                #include "DifficultyAnalyser"

                I simply the following in the SokoGenerator header
                class DifficultyAnalyser;
                DifficultyAnalyser* diffAnalyser;

                I then included the DifficultyAnalyser header in the SokoGenerator cpp file, and created a new instance of it
                #include "difficultyanalyser.h"
                diffAnalyser = new DifficultyAnalyser;

                And in the header for DifficultyAnalyser I included SokoGenerator
                #include "SokoGenerator.h"

                Thanks for your help!

                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