0

I have the following error :

Error : Undefined Reference to "Image2Grey::Image2Grey(int, int)"

I have a template Image2, and a class Image2Grey : Image2

The arborescence is :

\-folder1/
    \image2.h
    \image2.hpp
    \image2Grey.cpp
    \image2Grey.hpp
\-folder2/
    \main.cpp

When I try to create a new object Image2Grey, I have the error, like if my constructor does not exist. I don't understand what i am doing wrong.

Here is my code :

/** main.cpp **/

#include "../folder1/image2grey.hpp"
int main(int argc, char *argv[]) {
    Image2Grey img(6, 10);
    return 0;
}

/** image2.h **/

template<typename T>
class Image2D {
    public:
        Image2D(int width, int height);
    protected:
        T *image;
};

/** image2.hpp **/

#include "image2d.h"
template<typename T>
Image2D<T>::Image2D(int width, int height){
    image = new T[width * height];
}

/** image2grey.hpp **/

#include "image2d.h"
class Image2Grey : Image2D<unsigned char> {
public:
    Image2Grey(int width, int height);

/** image2grey.cpp **/

#include "image2grey.hpp"
Image2Grey::Image2Grey(int width, int height) :
    Image2D<unsigned char>(width, height) {}

If you have any idea about why I have this error.. Thanks

Marek R
  • 32,568
  • 6
  • 55
  • 140
iAmoric
  • 1,787
  • 3
  • 31
  • 64
  • 1
    This is linking error, not compile error. Your project file doesn't say that `image2Grey.cpp` should be compiled and linked. Depending what you are using problem may be in `CMakeList.txt` or `*.pro` file or visual studio project or … … . – Marek R Nov 14 '17 at 10:03
  • You are including "image2d.h"... Hope you have that file present. – Swanand Nov 14 '17 at 10:05

0 Answers0