I get a linking error when I am trying to build and compile a class which uses template class. I am using smart pointer to create an instance of this template class.
I have tried removing the int from main class instead of template T, and it seems to help but doesn't fixes the issue.
My vehicle class -
#ifndef VEHICLE_H
#define VEHICLE_H
template <class T>
class Vehicle
{
private:
T m_x;
T m_y;
public:
Vehicle(T x, T y);
Vehicle();
~Vehicle();
T add();
};
#endif
My vehicle.cpp
#include <iostream>
#include "pch.h"
#include "Vehicle.h"
template <class T>
Vehicle<T>::Vehicle(T x, T y): m_x(x), m_y(y)
{
std::cout << "Parameterised constructor called" << std::endl;
}
template <class T>
Vehicle<T>::Vehicle()
{
std::cout << "Calling default constructor" << std::endl;
}
template <class T>
Vehicle<T>::~Vehicle()
{
std::cout << "Calling destructor" << std::endl;
}
template <class T>
T Vehicle<T>::add()
{
return m_x + m_y;
}
Here is my main()
#include <iostream>
#include <memory.h>
#include "Vehicle.h"
int main()
{
std::cout << "Hello World!\n";
auto vehicle_ptr = std::make_unique<Vehicle<int>>(10,20);
std::cout << vehicle_ptr->add()<<std::endl;
return 0;
}
Linking Error I get, I expect it to compile and build.