2015-06-02 215 views
0

在我的应用程序中,我需要在不同时间在不同线程中使用几个函数,并且我不想将它们复制粘贴到任何地方。Qt C++ - 未定义的引用...内联QImage自定义函数

所以我做了一个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文件,而是一个),这不是做的事情,我思的好方法,和b)不会让我包括函数在不同的线程中。

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

+0

这是一个链接错误。编译和链接'commonfunctions.cc'应该可以解决它。 –

+0

commonfunctions.cc? – Petersaber

+0

你是如何编译你的项目的? –

回答

4

我不认为你可以在.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.

+0

奇怪。以前我有一个无头文件* .cpp文件的功能,它的工作...当我不得不做更多的线程(因为你不能共享* .cpp文件,只有头文件)出现问题。虽然你的答案解决了我的问题。将定义放在头文件中并从* .cpp中移除它已经完成了工作 - 我认为程序已经编译并且所有的线程和对象都可以访问这个函数。该应用程序编译。 – Petersaber

0

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

+0

但是为什么只有一个功能会导致问题呢? – Petersaber