0

I want to implement a template in a class file instead of implement all the code in the same file. But the problem is I have a linked error and I don't know what to do in order to solve it ? If you have any idea in order to help it will be graceful.

Main.cpp

#include <iostream>
#include <string>
#include "Myclass.h"
using namespace std;

int main()
{
    Myclass<int> firstObject;
    firstObject.setValue(2);
    int nbr = firstObject.getValue();


    system("PAUSE");
    return 0;
}

Myclass.cpp

#include "Myclass.h"


template <class Temp>
Myclass<Temp>::Myclass()
{
}

template <class Temp>
void Myclass<Temp>::setValue(Temp a)
{
    first = a;
}

template <class Temp>
Temp Myclass<Temp>::getValue()
{
    return first;
}

Myclass.h

#pragma once
template <class Temp>

class Myclass
{
public:
    Myclass();
    void setValue(Temp a);
    Temp getValue();
private:
    Temp first;
};
Ronycohen
  • 23
  • 4
  • You can't practically have an unspecialized template implementation in a source file. It's technically valid, but you won't be able to use it. – Gilad Naaman Oct 01 '16 at 18:01
  • I read the answer that you put in the link. But I don't understand. – Ronycohen Oct 01 '16 at 18:06
  • There are actually multiple answers on the question I linked to answering the reasons why putting template code into a separate file in multiple different ways (or what is needed to actually put them into a separate source). There seems to be no point in adding more responses (also, there are actually more questions for the same thing if you actually look at existing questions). – Dietmar Kühl Oct 01 '16 at 18:29

0 Answers0