0

I have some classes in my project, and when I try to compile my code, I am getting an error:

invalid use of incomplete type "class GameObject"

... when trying to inherit from GameObject. I have all #includes and forward declarations, but still have a problem. How do I solve it?

gameobject.h

#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H

#include "mainwindow.h"

class MainWindow;

class GameObject // base class
{
public:
    GameObject();
    QPoint getStartPos() const;
    QPoint getEndPos() const;

protected:
    std::string url;
    QLabel* label;
    MainWindow* window;
    QPoint startPos;
    QPoint endPos;
};

#endif // GAMEOBJECT_H

barrirer.h

#ifndef BARRIRER_H
#define BARRIRER_H

#include "gameobject.h"
#include "mainwindow.h"
#include <QLabel>
#include <QPoint>


class MainWindow;
class GameObject;

class Barrier : public GameObject // derived class, error here
{

public:
    Barrier(QLabel* _label, QPoint _startPos, QPoint _endPos,  MainWindow* _window);
    bool isMovePosible(QPoint _startPos, QPoint _endPos);
    bool isCrossed(QPoint _startPos, QPoint _endPos);
};

#endif // BARRIRER_H

mainwindow.h

part of it

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "barrier.h"

namespace Ui {
class MainWindow;
}

class Barrier;

class MainWindow : public QMainWindow
{
    Q_OBJECT

It's not all mainwidow.h, but lower there is absolutely cannot be any problem, there's no usage of GameObject in MainWindow

dodle
  • 1
  • 1
  • 3
    Does `mainwindow.h` include `gameobject.h` or `barrier.h`? – Jan Schultke Jun 14 '23 at 10:42
  • 4
    There is no "mainwindow.h" to be seen. A wild guess: it contains `#include "gameobject.h"`. – n. m. could be an AI Jun 14 '23 at 10:42
  • Probably the common circular include chain. Delete the include directives, and do only the *forward declarations* of the classes instead. – Some programmer dude Jun 14 '23 at 10:43
  • 1
    please read about [mcve]. You need less from what you did include (some code can be removed to get the same error) but the missing header (`mainwindow.h`) makes it impossible for others to know for sure what the issue is – 463035818_is_not_an_ai Jun 14 '23 at 10:50
  • also you should include the complete error message. It includes the line number and name of file where the error occured among other useful information – 463035818_is_not_an_ai Jun 14 '23 at 10:51
  • 2
    It looks like you're trying to avoid circularity problems by using forward declarations, but missed the point that you *shouldn't* include the headers that define those classes. Rule of thumb: no file should include headers that it doesn't need. – molbdnilo Jun 14 '23 at 11:03
  • Remove `#include "mainwindow.h"` from `barrirer.h` and make sure that `GameObject.h `does not include `mainwindow.h` – drescherjm Jun 14 '23 at 11:06
  • 5
    Having `class MainWindow;` (a forward declaration) and `#include "mainwindow.h"` in the same header file makes no sense at all. There's no point in a forward declaration unless it **replaces** the complete declaration. – john Jun 14 '23 at 11:06
  • 2
    The problem is the header loops. This question should help you remove the circular header loops: [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Jun 14 '23 at 13:47
  • "there is absolutely cannot be any problem" famous last words #249. – n. m. could be an AI Jun 14 '23 at 14:36
  • This question should be reclosed as a duplicate of https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes – François Andrieux Jun 14 '23 at 14:47

0 Answers0