如何在Qt中正确添加类(.cpp和.h文件)

时间:2017-05-01 16:09:44

标签: c++ qt linker

所以我在Visual Studio中编写了一些类(他们在VS中工作)并将它们添加到Qt中。当我指向一个班级时,一切都没问题。但是,如果我上课,我会得到2个关于2个方法的“未解决的外部符号”错误(类中只有2个)。其中一个看起来像这样:

mainwindow.obj:-1: error: LNK2001: unresolved external symbol "protected: virtual void __cdecl SRT::processInputLine(class Subtitles &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?processInputLine@SRT@@MEAAXAEAVSubtitles@@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

以下是此方法的声明:

virtual void processInputLine(Subtitles& subtitles, const string& line) throw(TimesOverlap) override;

Class Subtitles继承vector,其中Subtitle是另一个类。

如果我将定义粘贴到此文件中(我知道我不应该这样做),我只会为这些方法使用的其他类获得相同的错误,但不会导致之前的2个错误。 Qt没有正确链接.cpp文件吗?为什么只有在我创建类时才会出现此链接错误,而不是之前?有任何想法吗?谢谢!

Edit1:这是我的.pro文件:

    # deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
    mainwindow.cpp \
    Subtitles.cpp \
    MicroDVD.cpp \
    MPlayer.cpp \
    Subtitle.cpp \
    TimeStamp.cpp \
    SRT.cpp \
    SubtitleIO.cpp

HEADERS  += mainwindow.h \
    globalVariables.h \
    Exceptions.h \
    MicroDVD.h \
    MPlayer.h \
    Subtitle.h \
    Subtitles.h \
    TimeStamp.h \
    SRT.h \
    SubtitleIO.h

FORMS    += mainwindow.ui

DISTFILES += \
    SubtitleEditor.pro.user

如您所见,包括.h和.c文件。我正在试图弄清楚如何运行qmake ......

2 个答案:

答案 0 :(得分:1)

您应该将它们添加到您的专业文件中,如:

HEADERS += yourClass.h
SOURCES += yourClass.cpp

运行qmake,它应该编译好。

Qmake

点击构建 - &gt; 运行qmake

快速说明:qmake为您的项目创建makefile。因此,无论何时添加类,都应该运行它以确保所有内容都得到更新。

答案 1 :(得分:1)

  

我试图找出如何运行qmake ...

在控制台上使用qmake.exe yourProFile.pro。或者,所有IDE都提供了运行qmake的方法。如果您使用的是Qt Creator,请致电Build > Run qmake

相关问题