Qt C++ - 对 ... 内联 QImage 自定义函数的未定义引用

Qt C++ - undefined reference to ... inline QImage custom function

在我的应用程序中,有几个函数需要在不同时间在不同线程中使用,我不想将它们复制粘贴到各处。

所以我制作了一个 commonfunctions.cpp 和一个 commonfunctions.h,并将它们包含在不同的地方。但是,一个(出于许多)功能拒绝工作。

错误:

undefined reference to `CommonFunctions::cvMatToQImage(cv::Mat const&)'

commonfunctions.cpp里面有这个函数:

inline QImage CommonFunctions::cvMatToQImage(const cv::Mat &inMat) {
    (....)
}

commonfunctions.h

#ifndef COMMONFUNCTIONS
#define COMMONFUNCTIONS

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QImage>

#include "opencv/cv.h"
#include "opencv/highgui.h"

class CommonFunctions
{
public slots:
    inline QImage cvMatToQImage(const cv::Mat &inMat);
    void morphImage(cv::Mat threshold);
    void detectingMarkers(cv::Mat threshold, cv::Mat frame, cv::Mat roi,
                          int markerSizeMin, int markerSizeMax, int markerNumber,
                          int roiX, int roiY, bool tracking,
                          int &liczbaZgubionych, QString &komunikat);
};

#endif // COMMONFUNCTIONS

kalibracja.cpp中的调用

QImage image(commonFunctions.cvMatToQImage(frame));

我没有忘记 #include "commonfunctions.h"CommonFunctions commonFunctions; kalibracja.cpp

编译器知道它必须编译所有这些。 (*.pro 文件)

SOURCES += main.cpp\
    mainwindow.cpp \
    kalibracja.cpp \
    guiKalibracja.cpp \
    commonfunctions.cpp \
    analiza.cpp

HEADERS  += mainwindow.h \
    kalibracja.h \
    analiza.h \
    commonfunctions.h

当我简单地包含一个 *.cpp 文件时它起作用了,但是 a) 这不是做事的好方法,我认为,和 b) 它不允许我在不同的线程中包含函数。

什么可能导致此错误?调用相关函数的正确方法是什么?

您需要编译kalibracja.cppcommonfunctions.cpp,然后link将它们一起编译。

我认为您不能在 .cpp 文件中声明内联成员函数。摘自 another answer:

Note: It's imperative that the function's definition (the part between the {...}) be placed in a header file, unless the function is used only in a single .cpp file. In particular, if you put the inline function's definition into a .cpp file and you call it from some other .cpp file, you'll get an "unresolved external" error from the linker.