Qt创建者 - “qMain”错误的多重定义

时间:2012-05-30 12:48:47

标签: c++ qt

我开始使用QT创建器来完成我的作业制作GUI,但是我发布了这个错误,我无法找到它的原因,也无法理解它的含义。我想它会看到我的主要功能两次,但我不知道为什么......请协助我解决这个错误:

错误:

Makefile.Debug:155: warning: overriding commands for target `debug/main.o'
    Makefile.Debug:142: warning: ignoring old commands for target `debug/main.o'
    debug/main.o: In function `Z5qMainiPPc':
    D:\c++\Labs\GUI_r/../../../info/qt/Desktop/Qt/4.8.1/mingw/include/QtGui/qwidget.h:494: multiple definition of `qMain(int, char**)'
    debug/main.o:D:\c++\Labs\GUI_r/main.cpp:7: first defined here
    collect2: ld returned 1 exit status

代码:

#include <QtGui/QApplication>
#include "mainwindow.h"
#include "controller.h"
#include "StudentRepository.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    StudentRepository *stre = new StudentRepository();
    Controller *c = new Controller(stre);
    MainWindow w(c);
    w.show();

    return a.exec();
}

编辑:删除长代码 - 不是错误的原因。检查答案是否有用。

4 个答案:

答案 0 :(得分:11)

链接错误的原因是因为awkawrd代表QT创建者。我在projectName.pro -

QT       +=    core gui

TARGET = GUI_r
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    main.cpp \                   /////// Double call of main.cpp
    StudentRepository.cpp \
    controller.cpp

HEADERS  += mainwindow.h \
    controller.h \
    StudentRepository.h \
Student.h \
ui_mainwindow.h \        /////Double call of ui_mainwindow.h 
ui_mainwindow.h

FORMS    += mainwindow.ui

谢谢,我希望这篇文章对QTcreator的其他新用户有用。

答案 1 :(得分:5)

也许您的项目包含另一个带有main的源文件。某处文件重复。检查.pro文件中的“SOURCES =”和main.cpp。

答案 2 :(得分:2)

每个程序只能一个 QApplication

检查您的类(Controller,StudentRepository,MainWindow)并确保它们不会声明QApplication

答案 3 :(得分:1)

确实会看到qMain的两个定义,而不是您的主要定义。

您可能已经采用了示例程序并通过添加代码对其进行了修改。重新创建这些步骤,看看它何时停止工作。 编写代码时,尽可能经常进行编译,以便在引入后立即找到这些错误。