I am starting to learn Class Templates so I wrote a simple Header file which has the declaration and .cpp that contains the body.
HEADER FILE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef CLASS_H
#define CLASS_H
#include <iostream>
using std::cout;
namespace JuanSanchez
{
template <typename T>
class Array
{
public:
Array(T Var1);
T multiply(T SecElements);
private:
T elements;
};
}
#endif
.CPP FILE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// THIS IS Array.CPP
#include <iostream>
using std::cerr;
#include "Array.h"
// Constructor of the class Shape, to trace what is getting contructed a cout is added
// but in the real program the cout is removed.
template <typename T>
JuanSanchez::Array<T>::Array(T LocVar1)
{
cout << "Array Constructor called\n";
cout << LocVar1 <<'\n';
}
template <typename T>
T JuanSanchez::Array<T>::multiply(T Selements)
{
return elements * Selements;
}
1. These 2 files compile. But when I added the main I get unresolve external
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "Array.h"
usingnamespace JuanSanchez ;
int main()
{
double MY_PARAM = 50;
Array<double> myArray(MY_PARAM);
// cout << myArray.multiply(MY_PARAM) << '\n';
// This is just a pause so we could freeze the display
std::cin.get();
return 0;
}