我的代码存在问题。编译器会为我创建的每个CButton指针显示此错误。
/home/trafel/ドキュメント/Projects/C++/CCalc/ccalc.h:23: error : 'CButton' does not name a type
CButton *button_0;
^
CButton类从QPushButton扩展而来。 以下是主标题中的代码:
#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
这是来自CButton类标题:
#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
我忘了添加一些东西吗?
答案 0 :(得分:1)
简短解决方案:
在档案"cbutton.h"
中替换:
#include "ccalc.h"
使用:
class CCalc;
或完全删除#include
,因为无论如何您似乎都没有使用它。
<强>解释强>
请注意,您有循环依赖关系:在"ccalc.h"
中您需要知道CButton
是什么,而在"cbutton.h"
中您需要知道CCalc
是什么。
这意味着如果首先解析"cbutton.h"
,您将按此顺序点击这些行(仅显示相关行):
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.
在这些情况下的解决方案是,如果在"ccalc.h"
中您不需要知道"cubutton.h"
中的方法,而只是知道该类存在,请将其替换为类原型:class CButton;
。同样适用于#include "clabel.h"
和#include "clabel.h"
。