Ok, first problem taken care of, but why these errors? They go away if I inline the functions in the header file. Thanks for any help.
Header file
Code:
#include <iostream>
using namespace std;
template <class T>
class Array
{
public:
Array(int = 10);
virtual ~Array() {delete [] ptr;}
T &operator[](int);
int getSize() const {return size;}
private:
T *ptr;
int size;
};
Implementation
Code:
#include <iostream>
#include <cassert>
using namespace std;
#include "t_Array.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
template<class T>
Array<T>::Array(int arraySize)
{
size = (arraySize > 1 ? arraySize : 10);
ptr = new T[size] = {0};
assert(ptr != 0);
cout << "This is the class template version" << endl;
}
template<class T>
T &Array<T>::operator[](int subscript)
{
assert(1 <= subscript && subscript < size);
return ptr[subscript];
}
Driver
Code:
#include <iostream>
using namespace std;
#include "t_Array.h"
int main()
{
Array<double> tArray(15);
for (int i = 0; i < tArray.getSize(); i ++)
{
tArray[i] = i + 0.5;
cout << tArray[i] << endl;
}
return 0;
}
Errors
Code:
Compiling...
t_Array.cpp
Linking...
TempArray.obj : error LNK2001: unresolved external symbol "public: double & __thiscall Array<double>::operator[](int)" (??A?$Array@N@@QAEAANH@Z)
TempArray.obj : error LNK2001: unresolved external symbol "public: __thiscall Array<double>::Array<double>(int)" (??0?$Array@N@@QAE@H@Z)
Debug/TempArray.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
TempArray.exe - 3 error(s), 0 warning(s)