Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid use of incomplete type and forward declaration

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;
...
like image 912
alop789312 Avatar asked Feb 13 '23 21:02

alop789312


2 Answers

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()));
}
like image 154
thuga Avatar answered Feb 15 '23 11:02

thuga


Your forward declaration appears in namespace UI, but your class declaration appears outside of this namespace. You should change this to

 namespace UI {
     class MainWindow : public QMainWindow {
         Q_OBJECT
         // ...
     };
 }

It also seems you don't need to provide a forward declaration there at all, but change the forward declaration in Algorithm.h again to appear MainWindow in the right namespace:

 namespace UI {
     class MainWindow;
 }
like image 27
πάντα ῥεῖ Avatar answered Feb 15 '23 09:02

πάντα ῥεῖ