【问题标题】:How to properly add classes(.cpp and .h files) in Qt如何在 Qt 中正确添加类(.cpp 和 .h 文件)
【发布时间】:2017-05-02 02:17:31
【问题描述】:

所以我在 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 继承向量,其中 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...

【问题讨论】:

    标签: c++ qt linker


    【解决方案1】:

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

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

    运行 qmake,它应该可以正常编译。

    点击构建 -> 运行 qmake

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

    【讨论】:

    • 它们已添加到 pro 文件中,我已使用 .pro 文件的内容编辑了问题。
    • 谢谢伙计,我花了一整天的时间试图弄清楚我的代码出了什么问题,哈哈
    • @DusanMilosevic 保证你不会再被这个卡住:)
    【解决方案2】:

    我正在尝试弄清楚如何运行 qmake...

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

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      相关资源
      最近更新 更多