gpt4 book ai didi

c++ - [Qt] : 'CButton' does not name a type

转载 作者:行者123 更新时间:2023-11-28 05:39:24 35 4
gpt4 key购买 nike

我的代码有问题。对于我创建的每个 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 类 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

我是不是忘了添加一些东西?

最佳答案

简短的解决方案:

在文件 "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" 中的方法,但是只是为了知道该类存在,用一个类原型(prototype)替换它:class CButton;。这同样适用于 #include "clabel.h"#include "clabel.h"

关于c++ - [Qt] : 'CButton' does not name a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37494951/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com