Raphael Schimchowitsch Raphael Schimchowitsch - 1 month ago 13
C++ Question

Compilation error when not using template [opencv]

I have this part of code

static void translateMod(cv::Mat& imgIn, cv::Mat& imgOut, int nb_pixels) {
int w = imgIn.cols, h = imgIn.rows;
if (nb_pixels >= w) nb_pixels = w;
cv::Rect left(0, 0, w - nb_pixels, h);
cv::Mat ROI = imgIn(left);
cv::imshow("ROI", ROI);
}


The problem is that when I compile this, I have error mentionning that
"imshow is not a member of 'cv'"
, but when I add a template like this

template <typename T>
static void translateMod(cv::Mat& imgIn, cv::Mat& imgOut, int nb_pixels) {
int w = imgIn.cols, h = imgIn.rows;
if (nb_pixels >= w) nb_pixels = w;
cv::Rect left(0, 0, w - nb_pixels, h);
cv::Mat ROI = imgIn(left);
cv::imshow("ROI", ROI);
}


it works and I can compile and even execute.

Do someone know what happens here? :/

Thanks for all

Answer

The problem is that in the first (non-template) case, you are missing some #include before you implement the function.

In the second case it works because somewhere between where the function is implemented but before it is called, you have the necessary #include for imshow().

So figure out where imshow() is declared, and add that header file above the function definition.

The reason this happens is that the function template is not compiled until it is used. So it has the chance to be influenced by things between its definition and its use. A regular function is compiled where it is defined.

Comments