Hi All,
I had some issues with implementing copy constructor using the templates. I had tried to implement a small program to check the functionality but still could not figure out what is going wrong. Here is the snippet of the test program.
**>**Array.h**>**
// This header file implement the array of integer class
#ifndef __ARRAY_H_
#define __ARRAY_H_
template<typename T>
class Array{
public:
Array(int len=10):_len(len),_data(new T[len]){}
~Array(){ delete[] _data;}
int const& length() const{return _len;}
int& operator[](int index){return _data[index];}
int const& operator[](int index)const{return _data[index];}
Array(const Array &a);
private:
int _len;
T* _data;
};
**Array.cpp**
// This Program implement the array class
#include <iostream>
#include "Array.h"
template <typename T>
Array<T>::Array(const Array<T> &a)
{
delete _data;
_data = new T[a._len];
memcpy(_data,a._data,sizeof(int)*(_len));
}
I am getting the following error while compiling the above progam. I have already define my main in main.cpp.
>**main.cpp**>
// This program implements the main function
#include <iostream>
#include "Array.h"
using namespace std;
int main()
{
Array<int> a(12);
Array<int> b(a);
int x;
a[10]=100;
cout<<"the value is"<<a[10];
return 0;
}
Error::
/tmp/ccjT3hLN.o: In function `main':
/home/mukesh/Documents/Programs/03052011/main.cpp:8: undefined reference to `Array<int>::Array(Array<int> const&)'
collect2: ld returned 1 exit status
mukesh@mukesh-desktop:~/Documents/Programs/03052011$ g++ -g Array.cpp main.cpp
/tmp/cczBn5JC.o: In function `main':
/home/mukesh/Documents/Programs/03052011/main.cpp:8: undefined reference to `Array<int>::Array(Array<int> const&)'
collect2: ld returned 1 exit status