0

I have issues with my code. The compiler keeps showing this error for every CButton pointer I created.

/home/trafel/ドキュメント/Projects/C++/CCalc/ccalc.h:23: error : 'CButton' does not name a type
     CButton *button_0;
     ^

The CButton class extends from QPushButton. Here is the code from the main header:

#ifndef CCALC_H
#define CCALC_H

#include <QWidget>
#include <QMainWindow>
#include <QPushButton>
#include "clabel.h"
#include "cbutton.h"

class CCalc : public QMainWindow
{
    Q_OBJECT

public:
    CCalc();
    ~CCalc();

public slots:
    void CPush(QString);
    void CAction(QString);

private:
    CButton *button_0;
    CButton *button_1;
    CButton *button_2;
    CButton *button_3;
    CButton *button_4;
    CButton *button_5;
    CButton *button_6;
    CButton *button_7;
    CButton *button_8;
    CButton *button_9;
    CButton *button_equal;
    CButton *button_plus;
    CButton *button_minus;
    CButton *button_multiply;
    CButton *button_divide;
    CButton *button_power;
    CButton *button_root;
    CButton *button_leftParenthesis;
    CButton *button_rightParenthesis;
    CButton *button_return;
    CButton *button_ce;

    CLabel  *label_input;
    CLabel  *label_output;

    QMenu *menu_session;
        QAction *session_new;
        QAction *session_open;
        QAction *session_save;
        QAction *session_quit;
    QMenu *menu_log;
        QAction *log_inspect;
    QMenu *menu_help;
        QAction *help_help;
        QAction *help_about;
};

#endif // CCALC_H

And this is from the CButton class header :

#ifndef CBUTTON_H
#define CBUTTON_H

#include <QPushButton>
#include "ccalc.h"

class CButton : public QPushButton
{
    Q_OBJECT

public:
    CButton(QWidget *parent);
    ~CButton();
    QString getValue();
    void setValue(QString string);

private:
    QString value;
};

#endif // CBUTTON_H

Did I forget to add something ?

Daniel
  • 21,933
  • 14
  • 72
  • 101
  • 1
    `ccalc.h` includes `cbutton.h`, but `cbutton.h` includes `ccalc.h`. That's not a good idea: http://stackoverflow.com/questions/14909997/why-arent-my-include-guards-preventing-recursive-inclusion-and-multiple-symbol. It looks like you could just remove the include of `ccalc.h` from `cbutton.h`. – Alan Stokes May 28 '16 at 03:47

1 Answers1

1

Short solution:

In file "cbutton.h" replace:

#include "ccalc.h"

with:

class CCalc;

or remove that #include altogether as it seems you don't use it anyway.

Explanation:

Notice you have circular dependencies: in "ccalc.h" you need to know what a CButton is, and in "cbutton.h" you need to know what a CCalc is.

This means if "cbutton.h" is parsed first, you will hit these lines in this order (only relevant lines shown):

1. #define CBUTTON_H      <-- from file "cbutton.h"
2. #include "ccalc.h"     <-- from file "cbutton.h"
3. #include "cbutton.h"   <-- from file "ccalc.h", but as CBUTTON_H is already defined, the content of this file is ignored.
4. CButton *button_0;     <-- from file "ccalc.h", which indeed refers to an undefined class, as we have not reached the line "class CButton" yet.

The solution in those cases is, if in "ccalc.h" you don't need to know the methods that are in "cubutton.h", but merely to know the class exists, replace that include with a class prototype: class CButton;. Same applies to #include "clabel.h" and #include "clabel.h".

Daniel
  • 21,933
  • 14
  • 72
  • 101