0

In my app, there are a couple of functions that I need to use in different threads at different times, and I don't want to copy-paste them everywhere.

So I've made a commonfunctions.cpp and a commonfunctions.h, and included them in different places. However, one (out of many) function refuses to work.

The error:

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

commonfunctions.cpp has this function in it:

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

The call in kalibracja.cpp

QImage image(commonFunctions.cvMatToQImage(frame));

I didn't forget to #include "commonfunctions.h" or CommonFunctions commonFunctions; in kalibracja.cpp

The compiler knows it has to compile all of this. (*.pro file)

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

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

when i simply included a *.cpp file it worked, but a) that's not a good way to do things, Ithink, and b) that won't let me include the functions in different threads.

What could be causing this error? What would be the correct way to call the function in question?

Petersaber
  • 851
  • 1
  • 12
  • 29

2 Answers2

4

I don't think that you can have inline member functions declared in .cpp files. Taken from 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.

Community
  • 1
  • 1
Mitch
  • 23,716
  • 9
  • 83
  • 122
  • Strange. Previously I had the function in a header-less *.cpp file and it worked... problem appeared when I had to make more threads (as you can't share *.cpp files, only headers). Your answer solved my problem, though. Putting the definition inside the header file and removing it from *.cpp has done the job - the program compiled and all threads and objects have access to the function, I think. The app compiled. – Petersaber Jun 02 '15 at 12:07
0

You need to compile both kalibracja.cpp and commonfunctions.cpp, and then link them together.

Jonas
  • 6,915
  • 8
  • 35
  • 53