1

This is declaration file MyImage.h:

#pragma once

template <class Pixel>
class MyImage
{
public:
    MyImage();
    MyImage(unsigned int w, unsigned int h);
    MyImage(const MyImage<Pixel>& copie);
    ~MyImage();
    MyImage<Pixel>& operator=(const MyImage<Pixel>& image); //quand on utilise  la variable MyImage, il faut écrire MyImage<Pixel>
    int getWidth() const;
    int getHeight() const;
    //resize(int w, int h);
    //T* operator[](int){return _bitmap+ i*_width;}  adresse du 1er element de la ligne

private:
    int _width;
    int _height;
    Pixel *_bitmap;

};

This is definition file MyImage.cpp:

#include "MyImage.h"

template <class Pixel>
MyImage<Pixel>::MyImage() : _width(0), _height(0), _bitmap(NULL)
{}

template <class Pixel>
MyImage<Pixel>::MyImage(unsigned int w, unsigned int h) : _width(w), _height(h)
{
    _bitmap = new Pixel[w*h];
}

template <class Pixel>
MyImage<Pixel>::MyImage(const MyImage<Pixel>& copie) : MyImage(_width, _height)
{
    for (int i = 0; i < _width*_height; i++)
    {
        _bitmap[i] = copie._bitmap[i];
    }
    // ou _bitmap = new bitmap(*(copie._bitmap));
}

template <class Pixel>
MyImage<Pixel>::~MyImage()
{
    delete _bitmap;
}

template <class Pixel>
MyImage<Pixel>& MyImage<Pixel>::operator=(const MyImage<Pixel>& image)
{
    if (this != &image)
    {
        _width = image._width;
        _height = image._height;
        delete _bitmap;
        for (int i = 0; i < _width*_height; i++)
        {
            _bitmap[i] = copie._bitmap[i];
        }
        // ou _bitmap = new bitmap(*(copie._bitmap));
    }
    return *this;
}

template <class Pixel>
int MyImage<Pixel>::getWidth() const
{
    return _width;
}
template <class Pixel>

int MyImage<Pixel>::getHeight() const
{
    return _height;
}

This is main function main.cpp:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "MyImage.h"

using namespace std;

int main()
{
    MyImage<unsigned char> image(20,20);    //  0-255
    //MyImage<unsigned char> image1(image);
    return 0;
}

And I have some errors like as:

1>------ Build started: Project: TP5 Template Image, Configuration: Debug Win32 ------
1>  MyImage.cpp
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MyImage<unsigned char>::MyImage<unsigned char>(unsigned int,unsigned int)" (??0?$MyImage@E@@QAE@II@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MyImage<unsigned char>::~MyImage<unsigned char>(void)" (??1?$MyImage@E@@QAE@XZ) referenced in function _main
1>D:\TPCPP\TP5 Template Image\Debug\TP5 Template Image.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

In main function, I've tried to declare "MyImage image();" and it's ok but it doesn't work with "MyImage image(20,20);"

Anyone can help me, please ?

Dutchman
  • 41
  • 6
  • The implementation of templates can't be in .cpp files separated from the headers or you'll get these sorts of linker errors. There is probably another question on this site with a more detailed answer explaining why, but the basic solution is to move the implementation into the .h file. – templatetypedef Apr 13 '14 at 21:24
  • I place entire "MyImage.cpp" into "MyImage.h" and it works. Thank you very much – Dutchman Apr 13 '14 at 21:47
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Luchian Grigore Apr 14 '14 at 06:00

1 Answers1

0

Place class member function definitions in the same header where the template class is defined.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335