【问题标题】:ERROR: 'abc' does not have a name type QT c++ GUI application错误:“abc”没有名称类型 QT c++ GUI 应用程序
【发布时间】:2015-02-08 21:48:41
【问题描述】:

底线是我有 2 种形式,第一种主窗口第 2 种形式1。我在主窗口上有一个显示表单 2 的按钮。现在我在 form1 上有一个按钮,应该将我带到主窗口,但它不起作用。问题是当我在 form1.h 中说 #include 时,它​​给了我一个错误,即主窗口没有名称类型。请帮忙,如果可能的话,工作示例会很棒。实际错误是 MainWindow 没有名称类型

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <form1.h>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;   // i put this line of code in public section when i was trying ui->show(); in form1.cpp file 

    Form1 obj ; // to show next form

};

#endif // MAINWINDOW_H

mainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }

MainWindow::~MainWindow()
    {
        delete ui;
    }

void MainWindow::on_pushButton_clicked()
    {
        obj.show();
        this->hide();
    }

Form1.h

  #ifndef FORM1_H
#define FORM1_H
#include <QWidget>

#include<mainwindow.h>

namespace Ui {
    class Form1;
}

class Form1 : public QWidget
{
    Q_OBJECT

public:
    explicit Form1(QWidget *parent = 0);
    ~Form1();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Form1 *ui;

    MainWindow mw ;     // here i am making object of main window
};

#endif // FORM1_H

form1.cpp

#include "form1.h"
#include "ui_form1.h"

#include<mainwindow.h>   // i know when i include this there this issue occurs , but i want to go my previous form to show and for that i have to make its object ! thats how it works when i am going to my next form i.e form1 now i want to go back 



Form1::Form1(QWidget *parent) :
QWidget(parent),

ui(new Ui::Form1)
{
    ui->setupUi(this);
}

Form1::~Form1()
{
    delete ui;
}

void Form1::on_pushButton_clicked() // show mainWindow
{
    mw->show();
    this->hide();
    //MainWindow::ui->show();        // i even tried this
}

我也试过如果我可以通过 puttin Ui::MainWindow *ui; 在 form1.h 中不包含 mainwindow.h 来做到这一点在公共部分所以 form1.cpp 文件我可以通过键入 MainWindow::ui->show() 来访问它;这次 ERROR 说 Object is missing Reference to 'MainWindow::ui'

【问题讨论】:

  • mainWindow.h?一些操作系统使用区分大小写的名称(大多数 *nix 名称),因此可能会导致麻烦。另外,发布确切的错误。
  • @zeta,这是错误:'mainWindow' 没有名称类型
  • @Zeta 你可以看到代码。 IDE 自动生成主窗口的表单,因为它是第一个表单。所以我不认为区分大小写是真正的问题。我刚刚尝试了所有 #include , #include #include 和 #include 没有用 :( ! Zeta 谢谢 :)
  • @Zeta 我在 form.h 中尝试过
  • 向您的问题添加其他信息,而不是在 cmets 中。确保您的问题易于理解。目前它或多或少是一堵代码墙。尝试将其简化为基本要素(此外,在这一点上,我正在尝试改进您的问题,而不是回答它;那将是答案,而不是评论:D)。

标签: c++ forms qt user-interface


【解决方案1】:

MainWindowForm1 类之间存在循环依赖关系。因此,您在mainwindow.h 中包含form1.h,在form1.h 中包含mainwindow.h。当编译器到达该行时

MainWindow mw ;     // here i am making object of main window

这是他第一次遇到符号MainWindow并触发该错误。

我仍然不明白为什么mw 是Form1 的成员,但是您可以使用指针来打破依赖关系。基本上你现在会有

MainWindow* mw ;

而不是Form1.h 中的#include&lt;mainwindow.h&gt;,您只需转发声明

class MainWindow;

【讨论】:

  • 感谢您的回答 :) 我正在尝试实施一段时间,但它不起作用
  • 谢谢您的回答 :) 为什么 mw 是 Form1 的成员,因为 form1 表单上有一个按钮,在该按钮后面我想编写一个显示主窗口表单的代码,为此我必须在 form1.h 中创建主窗口对象,所以在 form1.cpp 中我可以使用它。我正在尝试实施一段时间,但它不起作用,它给出了另一个错误:请求成员 'show' in '((Form1*)this)->Form1::mw' ,这是非类类型'主窗口*'
猜你喜欢
  • 2013-09-11
  • 2017-04-10
  • 1970-01-01
  • 2018-12-09
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-01
相关资源
最近更新 更多