Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: invalid use of incomplete type and forward declaration

Tags:

c++

qt

I have some misunderstanding:

A.h

#ifndef A_H
#define A_H

#include "B.h"

class A : public B
{
    Q_OBJECT
public:
    A();
};
#endif

A.cpp

#include "A.h"
A::A()
{
    B::ui->blancH2->setValue(2);
}

B.h

#include <QWidget>
#ifndef B_H
#define B_H

namespace Ui {
    class B;
}
class B
{
    Q_OBJECT
public:
    explicit B(QWidget *parent = 0);

public:
    Ui::B *ui;
};

#endif 

As result of compiling I have next errors: A.cpp: In constructor 'A::A()': invalid use of incomplete type 'class Ui::B' B.h: forward declaration of 'class Ui::B'

Can anybody explain why I have this errors?

like image 370
ibn_maksimys Avatar asked Nov 28 '22 09:11

ibn_maksimys


2 Answers

Check you ui_B.h. At the end of it, you should see

namespace Ui {
    class B: public Ui_B {};
}

If it's not, you can open you .ui file in Qt Designer, select your widget, and in Object Inspector, change the string under 'Object' to 'B'. The default value is 'Dialog' if your widget is a dialog.

Don't modify ui_B.h directly since it's generated by Qt compiler and it will be overwritten every time you compile.

like image 190
user3739779 Avatar answered Dec 21 '22 09:12

user3739779


As the error says, there is no definition of Ui::B available, only the declaration in B.h; and you can't write code that accesses members of a class unless the class has been defined.

You need to include whichever header defines Ui::B; or, perhaps, give B a member function to do whatever A needs doing, rather than furtling with B's members directly.

like image 26
Mike Seymour Avatar answered Dec 21 '22 09:12

Mike Seymour