Forward Declaration of Class and Incomplete Type



  • 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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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



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


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



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