简体   繁体   English

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

[英]How to properly add classes(.cpp and .h files) in Qt

So I've written some classes in Visual Studio(they worked in VS) and I added them to Qt. 因此,我在Visual Studio中编写了一些类(它们在VS中工作),并将它们添加到Qt中。 When I Make a pointer to a class, everything's okay. 当我指向一个类的指针时,一切都很好。 If I make the class, however, I get 2 "unresolved external symbol" errors about 2 methods(there are only 2 in class). 但是,如果我上课,则会遇到2种关于2种方法的“无法解析的外部符号”错误(类中只有2种)。 One of them looks like this: 其中之一看起来像这样:

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)

Here's the declaration of this method: 这是此方法的声明:

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

Class Subtitles inherits vector, where Subtitle is another class. Class Subtitles继承了vector,而Subtitle是另一个类。

If I paste the definitions into this file(which I know I shouldn't do) I only get the same errors for other classes that these methods use, but not the previous 2 errors. 如果我将定义粘贴到此文件中(我知道我不应该这样做),那么对于这些​​方法使用的其他类,我只会得到相同的错误,而不会出现前两个错误。 Is Qt not linking .cpp files correctly? Qt是否无法正确链接.cpp文件? Why does this linking error occur only if I create class, and not before? 为什么仅当我创建类而不是之前创建类时才会发生此链接错误? Any ideas? 有任何想法吗? Thanks! 谢谢!

Edit1: Here's my .pro file: 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

As you can see .h and .c files are included. 如您所见,其中包括.h和.c文件。 I'm trying to figure out how to run qmake... 我试图弄清楚如何运行qmake ...

You should add them to your pro file like: 您应该将它们添加到pro文件中,如下所示:

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

run qmake and it should compile fine. 运行qmake,它应该可以正常编译。

Qmake

Click Build -> Run qmake 单击生成 -> 运行qmake

Quick explanation: qmake creates makefiles for your project. 快速说明:qmake为您的项目创建makefile。 Therefore whenever you add classes you should run it to assure that everything gets updated. 因此,无论何时添加类,都应运行它以确保所有内容都已更新。

I'm trying to figure out how to run qmake... 我试图弄清楚如何运行qmake ...

Use qmake.exe yourProFile.pro on the console. 在控制台上使用qmake.exe yourProFile.pro Alternatively all IDE provide a way to run qmake. 另外,所有IDE都提供一种运行qmake的方法。 If you are using Qt Creator call Build > Run qmake . 如果您使用的是Qt Creator,请调用Build > Run qmake

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM