Invalid use of incomplete type and forward declaration

c++, forward-declaration, qt

Solution

The problem in your code is that you're trying to use the `m_ui` member of `MainWindow` class in your `Algorithm` class, and the type of `m_ui` is `Ui::MainWindow`, but your `Algorithm` class is not aware of such type. You can solve this by including the `ui_mainwindow.h` in your `algorithm.cpp`.

But this is still poor design. `m_ui` should be a private member of `MainWindow` class. You should not be accessing it from other classes. Instead create functions in your `MainWindow` class that will allow you to do what you want.

For example, create a public function like this:

void MainWindow::addCustomAction(QAction *action)
{
    m_ui->menuAlgorithms->addAction(action);
}

Then call this function from your `Algorithm` class:

Algorithm::Algorithm(MainWindow *mainWindow)
{
   this->mainWindow = mainWindow;

   QAction *action = new QAction(this);
   action->setObjectName(QStringLiteral("action"));
   action->setText(this->getName());

   mainWindow->addCustomAction(action);

   connect(action, SIGNAL(triggered()), this, SLOT(start()));
}

Problem

I was trying to resolve this issue but there must be some misunderstanding in how I undestand forward declaration. I am getting following error: ``` src/algorithm.cpp: In constructor ‘Algorithm::Algorithm(MainWindow*)’: src/algorithm.cpp:22:20: error: invalid use of incomplete type ‘struct Ui::MainWindow’ src/mainwindow.h:23:10: error: forward declaration of ‘struct Ui::MainWindow’ ``` I have these files (i ommited some lines and files and pasted only relevant code): algorithm.cpp ``` #include "algorithm.h" #include "mainwindow.h" Algorithm::Algorithm(MainWindow *mainWindow) { this->mainWindow = mainWindow; QAction *action = new QAction(this); action->setObjectName(QStringLiteral("action")); action->setText(this->getName()); mainWindow->m_ui->menuAlgorithms->addAction(action); mainWindow->connect(action, SIGNAL(triggered()), this, SLOT(this->start())); } ``` algorithm.h ``` #ifndef ALGORITHM_H #define ALGORITHM_H #include <QObject> #include "graphwidget.h" #include "edge.h" #include "vertex.h" class MainWindow; class Algorithm : public QObject { public: MainWindow *mainWindow; Algorithm(MainWindow *mainWindow); void start(); virtual void solve(); virtual QString getDescription(); virtual QString getName(); }; ``` mainwindow.cpp ``` #include "mainwindow.h" #include "algorithm.h" #include "../ui/ui_mainwindow.h" #include "vertex.h" #include "edge.h" #include "warning.h" mode_type mode; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MainWindow) { gundirected = NULL; gdirected = NULL; m_ui->setupUi(this); ... ``` mainwindow.h ``` #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSystemTrayIcon> #include <QSignalMapper> #include <QUndoView> #include <QGraphicsView> #include <QGraphicsScene> #include <QKeyEvent> #include <QGraphicsSceneMouseEvent> #include <QLabel> #include <QVBoxLayout> #include <QPushButton> #include "graphwidget.h" enum mode_type {NORMAL, VERTEX, EDGE}; // vyctovy typ pro urceni editoacniho modu extern mode_type mode; namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); Ui::MainWindow *m_ui; ... ```

Original source