0

看起来我对使用 Qt 的 c++ 编程有点陌生,所以我已经在我的 win 7 64 位上下载了 5.0 版本。我创建了一个名为 Mafenetre 的类,我实现了它的代码,它看起来很完美,但是当我尝试运行主程序时,它显示 C:\Users\Zbart3i\Downloads\Programs\test\main.cpp:3: error: C1083:无法打开包含文件:“Mafenetre.h”:没有这样的文件或目录

这是我的专业代码:

SOURCES += \
    main.cpp \
    mafenetre.cpp

QT+=widgets

HEADERS += \
    mafenetre.h

这是 Mafenetre.h 的代码:

#ifndef MAFENETRE_H
#define MAFENETRE_H


include <   QtWidgets>

class Mafenetre:public QWidget
{
public:
    Mafenetre();
private:
    QPushButton *m_bouton;

};

#endif // MAFENETRE_H

这是 Mafenetre 的.cpp:

#include      "mafenetre.h"

Mafenetre::Mafenetre():QWidget()
{
    setFixedSize(300,150);

    m_bouton=new QPushButton("pimp mon bouton",this);

    m_bouton->setFont(QFont("monotype corsiva",15));
    m_bouton->setCursor(Qt::PointingHandCursor);

}

和 main.cpp

#include<    QtWidgets/QApplication>
#include<    QtWidgets>
#include<    Mafenetre.h>


void main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Mafenetre fenetre;


    fenetre.show();
    app.exec();

}
4

2 回答 2

0

In C++, you should use the #include "name", not #include (Quote marks instead of triangle brackets), as they have different meanings.

#include looks in certain directories where libraries (including the standard library) is installed. #include "name" looks first in your project's folders, and then checks the other directories.

Well, to be technically accurate, what folders they look in, and in one order, is compiler-specific.

This:

#include <Mafenetre.h>

Should be this:

#include "Mafenetre.h"

Read this:

MinGW #include search directories

Other Directories Searched by Default

The minimal list of directories, identified as described above, specifies the only locations which will be searched by default, for system header files or for headers associated with user installed libraries; however, there is one exception. Astute readers may have noticed that the include file search path is itemised in a pair of sequential lists, with the second being concatenated to the first; however, the first, identified as the #include "..." search list appears to be empty. In reality, this apparent emptiness is possibly misleading; unless the user specifies the "-I-" option when invoking GCC, this list contains exactly one directory: the directory in which the source file containing the #include "file" directive resides.

Notice that this one additional directory is not searched for headers specified with the #include <file> form of the #include directive; it applies only for those headers specified using the #include "file" form of the directive.

And make sure your "Mafenetre.h" file is in the same folder as your main.cpp file. Otherwise you need to do #include "folderPath/fileName.h", and also add the path to the SOURCES or HEADERS variables in the .pro file.

Also make sure the spelling is identical, and that the proper case is used - sometimes it matters, sometimes it doesn't.

If you are still having trouble, try to compile a simple project consisting of just a single main.cpp, to make sure everything is installed correctly.

于 2013-03-09T00:37:09.387 回答
0

你确定你已经包含了正确的标题。此外,我没有看到您在头文件中使用 QObject 宏。它应该是这样的:

Mafenetre.h:

#ifndef MAFENETRE_H
#define MAFENETRE_H

#include <QWidget>

    class Mafenetre : public QWidget
    {
        Q_OBJECT
    public:
        explicit Mafenetre(QWidget *parent = 0);

    signals:

    public slots:

    };

    #endif // MAFENETRE_H

Mafenetre.cpp:

#include "mafenetre.h"

Mafenetre::Mafenetre(QWidget *parent) :
    QWidget(parent)
{
}

最后你的 main.cpp 应该是这样的,检查标题:

......
#include "mafenetre.h"


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Mafenetre m;
    m.show();

    return a.exec();
}
于 2013-03-09T00:45:03.460 回答