0

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.

Prawn Hongs
  • 441
  • 1
  • 5
  • 17
  • Hi there Prawn. Which book are you using? It should explain this. (It has nothing to do with smart pointers) – Lightness Races in Orbit Apr 01 '19 at 00:22
  • @LightnessRacesinOrbit - what book I need to read to know these things? Any book which explains things in detail rather than assuming reader to read between the lines. – Prawn Hongs Apr 01 '19 at 00:53

0 Answers0