【问题标题】:undeclared identifier error with correct include and declaration正确包含和声明的未声明标识符错误
【发布时间】:2011-11-17 20:43:05
【问题描述】:

我正在 Qt 中创建一个应用程序 我得到一个编译错误:错误 C2065:'callDialog':未声明的标识符 在我的 CategoryDialog 类中 错误行:

CallDialog* callDialog = ((CallDialog*)dialogStack->widget(1));

CategoryDialog 类的倒数第五行:

    #include "ui_categorydialog_new.h"
    #include "calldialog.h"
    #include "questionsdialog.h"
    #include "../objects/call.h"
    #include "../webservice/dataconnector.h"
    #include "../webservice/dataconngetter.h"

    namespace Ui {
        class CategoryDialog;
    }

    class CategoryDialog : public QDialog
    {
        Q_OBJECT

    public:
        explicit CategoryDialog(QWidget *parent = 0) : QDialog(parent), ui(new Ui::CategoryDialog){
            ui->setupUi(this);
        }

        ~CategoryDialog()
        {
            delete ui;
        }

    private slots:
        void on_btn_back_clicked()
        {
            ui->btn_back->setEnabled(false);
            DataConnGetter::getConnector()->setCallAbort(call->getId()); //get errormessage back and show errormessage when nessesary
            QStackedWidget* dialogStack = (QStackedWidget*)this->parentWidget();
            CallDialog* callDialog = ((CallDialog*)dialogStack->widget(1)); //TODO 005 replace indexes with enums > more clear
            callDialog->updateCalls(false);
            dialogStack->setCurrentIndex(1);
            ui->btn_back->setEnabled(true);
        }

CallDialog 类如下所示

    #include <QDialog>
    #include <QString>
    #include <QList>
    #include <QSound>
    #include <QtDebug>
    #include <QStringList>
    #include <QPushButton>
    #include <QStackedWidget>
    #include <QtAlgorithms>
    #include <QLabel>
    #include <typeinfo>

    #include "ui_calldialog.h"
    #include "callbutton.h"
    #include "categorydialog_new.h"
    #include "../settings/consts.h"
    #include "../webservice/dataconnector.h"
    #include "../webservice/dataconngetter.h"
    #include "../settings/programsettings.h"
    #include "../webservice/pollingservice.h"

    class PollingService;

    namespace Ui {
        class CallDialog;
    }

    class CallDialog : public QDialog
    {
        Q_OBJECT

    public:
        //    explicit CallDialog(QWidget *parent = 0);
        //    ~CallDialog();
        //    void initCalls();
        //    void updateCalls(bool sound);
        //    void enablePoller();

        explicit CallDialog(QWidget *parent = 0) : QDialog(parent), ui(new Ui::CallDialog)
        {
            ui->setupUi(this);
            installEventFilter(this);

注意正确包含在 CategoryDialog 中

它们确实相互包含(可能是循环依赖问题?) 我尝试前向声明 CallDialog。没有帮助。

这些文件只是直接在内部实现的 .h 文件

编辑

我绕过这个问题如下: 我添加了一个抽象类,其中包含 CallDialog 从 CategoryDialog 使用的函数

像这样:

#ifndef ABSTRACTDIALOG_H
#define ABSTRACTDIALOG_H

#include <QDialog>

#include "../objects/call.h"

class AbstractDialog : public QDialog
{
    Q_OBJECT

public:
    explicit AbstractDialog(QWidget *parent = 0) : QDialog(parent){}
    void setCall(Call* call){
        _call = call;
    }
private:
    Call* _call;

};

#endif // ABSTRACTDIALOG_H

【问题讨论】:

  • 能否指出导致编译器错误的行号和文件?
  • 是的,我添加了错误行(第一个代码块中的倒数第五行)
  • 您能否将失败行更改为((CallDialog*)dialogStack-&gt;widget(1))-&gt;updateCalls(false); callDialog 变量在下一行仅使用一次,编译器错误抱怨callDialog,而不是CallDialog。很想知道更改后会产生什么错误,因为我没有看到代码有任何明显错误。
  • CallDialog类声明的右大括号后面是否有分号(应该有)?
  • @Berty 您能否上传编译器产生的所有错误并尽可能完成源代码。

标签: c++ compilation include declaration


【解决方案1】:

CategoryDialog 之前转发声明 CallDialog 将不起作用,因为使用了 CallDialog 成员函数并且编译器需要知道它们。

您是否尝试在 CallDialog 之前声明 CategoryDialog 并删除,我猜是 #include "categorydialog_new.h"?由于未显示所有calldialog.h,我不确定您如何在该文件中使用CategoryDialog

【讨论】:

  • 我试过了,但后来它说它不知道 CategoryDialog。我正在尝试解决循环包含问题。
  • @Berty 由于两个标头都需要使用另一个标头,为了解决这个问题,我相信您必须将实现放入 cpp 文件中。
  • 哈哈,我正在尝试摆脱 cpp 文件。它大大减慢了开发速度。不幸的是,它会产生其中一些错误。
猜你喜欢
  • 1970-01-01
  • 2013-03-12
  • 2013-01-22
  • 2011-10-25
  • 2011-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多