Qt(C++)的几个问题



main.cpp

#include <QtGui>
#include <QApplication>

int main(int argv, char **args)
{
    QApplication app(argv, args);
    QTextEdit textEdit;
    QPushButton quitButton("Quit");
    QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
    QVBoxLayout layout;
    layout.addWidget(&textEdit);
    layout.addWidget(&quitButton);
    QWidget window;
    window.setLayout(&layout);
    window.show();
    return app.exec();        
}

notepad.cpp

#include <QtGui>
#include <QApplication>
class Notepad : public QMainWindow
{

    Notepad::Notepad()
    {
        saveAction = new QAction(tr("&Open"), this);
        saveAction = new QAction(tr("&Save"), this);
        exitAction = new QAction(tr("E&xit"), this);
        connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
        connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
        connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
        fileMenu = menuBar()->addMenu(tr("&File"));
        fileMenu->addAction(openAction);
        fileMenu->addAction(saveAction);
        fileMenu->addSeparator();
        fileMenu->addAction(exitAction);
        textEdit = new QTextEdit;
        setCentralWidget(textEdit);
        setWindowTitle(tr("Notepad"));
    }
    Q_OBJECT
public:
    Notepad();
    private slots:
        void open();
        void save();
        void quit();
private:
    QTextEdit *textEdit;
    QAction *openAction;
    QAction *saveAction;
    QAction *exitAction;
    QMenu *fileMenu;
};

错误:

会员记事本(第8行(上的额外资格"记事本::">

notepad::notepad((不能重载(第32行(

带记事本::记事本(第8行(

为什么我会出现这些错误?构造函数看起来不错,类设置看起来不错。但我犯了这些错误。

Notepad类中Notepad()构造函数前面的Notepad::不是必需的。后面的声明也不是,因为您已经完成了这一点,并在上面定义了它(尽管是私下的(。您可能需要考虑将其分离为一个标头和cpp文件。

在您发布的代码中仍然存在各种其他问题,但您发布的错误很可能是由我上面提到的内容引起的。

  • 您已使用Notepad::限定内联私有构造函数
  • 然后,您在第二个声明中将该私有构造函数错误地重载为public
  • Q_OBJECT宏需要在类声明中位于方法和成员之前的第一个
  • 每个Notepad实例至少有4个内存泄漏
  • 等等

也许拿起一本书?

最新更新