Hello,

I am having a few problems with my image class using OpenCV.
I am getting the errors:

src/image.cpp: In constructor ‘Image<T>::Image(const char*, bool)’:
src/image.cpp:21: error: there are no arguments to ‘cvLoadImage’ that depend on a template parameter, so a declaration of ‘cvLoadImage’ must be available
src/image.cpp:21: error: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
src/image.cpp:23: error: there are no arguments to ‘cvLoadImage’ that depend on a template parameter, so a declaration of ‘cvLoadImage’ must be available
src/image.cpp: In member function ‘void Image<T>::save(const char*)’:
src/image.cpp:44: error: there are no arguments to ‘cvSaveImage’ that depend on a template parameter, so a declaration of ‘cvSaveImage’ must be available

The header file is pretty standard, including cv.h for the required functions.

Code snippets below, with full files attached (image.hpp renamed to image.h otherwise it would not allow upload):

image.cpp Lines 18-25:

template<class T>
   Image<T>::Image(const char* filename, bool color = true) {

       if(color) {

         imgp = cvLoadImage(filename);         

       } else {

         imgp = cvLoadImage(filename,0);     

       } 

   }

image.cpp Lines 42-45:

template<class T>
   
void Image<T>::save(const char* filename) {

       cvSaveImage(filename, imgp); 

   }

Thanks alot for any help :)
Adam

Dani AI

Generated

Two separate C++ template rules are at work here and explain both the compile-time complaints about the OpenCV calls and the later linker "undefined reference" messages.

Non-dependent (free) names like the OpenCV functions are looked up when the compiler parses the template definition. If the OpenCV declarations are not visible at that point (or the template definition lives in a translation unit that doesn’t include the headers), the compiler will complain. Separately, putting a template's full implementation in a .cpp file means the compiler won’t automatically emit code for concrete types used elsewhere, which produces the undefined-reference linker errors.

Practical fixes (pick one):

  • Make the template header-only: move the entire implementation into the header (or #include an implementation fragment from the header). This guarantees correct name lookup and that instantiations are available where needed.

  • Keep implementation in a .cpp but explicitly instantiate each concrete type used. Example (in the .cpp that has the definitions):

#include "image.h"    // declares template
// include OpenCV headers here if image.h doesn't
// Force generation of all member functions for RgbPixel:
template class Image<RgbPixel>;

Additional notes: ensure the OpenCV headers are included before the template definition (or in the header itself). For member names that depend on template parameters, use the dependent-name form (e.g., this->member) as suggested so the compiler resolves them at instantiation time. ’s remark that the “full implementation must be in the template class’s header” describes the typical, simplest solution; explicit instantiation is an alternative when a header-only layout is undesirable.

Recommended Answers

All 6 Replies

Have you put

template <class T>
class Image {

};

And where is Image.hpp?

Hello,

I have read through that link. I am still at a loss as to exactly what i have done wrong, and how i would go about fixing it.

Any advice is much appreciated
Adam

That link deals entirely with your exact problem and why it occurs, and tells you how to fix or avoid it. Please read it through carefully, again. this->imgp = cvLoadImage( filename ); cvSaveImage( filename, this->imgp ); Good luck.

Hello,

The compiler has no errors now, thank you :)

The linker on the other hand.... I am getting:

g++ main.o image.o -o tpp1 `pkg-config --cflags opencv` `pkg-config --libs opencv`
main.o: In function `main':
main.cpp:(.text+0x8a): undefined reference to `Image<RgbPixel>::Image()'
main.cpp:(.text+0x93): undefined reference to `Image<RgbPixel>::Image()'
main.cpp:(.text+0x13b): undefined reference to `Image<RgbPixel>::Image(char const*, bool)'
main.cpp:(.text+0x14c): undefined reference to `Image<RgbPixel>::~Image()'
main.cpp:(.text+0x1e7): undefined reference to `Image<RgbPixel>::~Image()'
main.cpp:(.text+0x200): undefined reference to `Image<RgbPixel>::~Image()'
main.cpp:(.text+0x212): undefined reference to `Image<RgbPixel>::~Image()'
main.cpp:(.text+0x237): undefined reference to `Image<RgbPixel>::~Image()'
collect2: ld returned 1 exit status

My files are attached. Code is mostly the same as first post bar a few fixes to make it compile.

Thanks
Adam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.