My method constructor accepts a template parameter value and I know templates can be used in the implementation file (.cpp) by preceding each method with
template<class T>;
classname<T>::memberfunction(){
}
When i put the definition of my constructor together with the class definition, the error doesn't show. For learning purposes, i want to put the implementation on a seperate file .cpp
I'm getting this error:
Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __cdecl myclass::myclass(int)" (??0?$myclass@H@@QEAA@H@Z) referenced in function main ConsoleTestProject C:\Users\Winux\Desktop\MINE\Project\Lab1\ConsoleTestSolution\ConsoleTestProject\ConsoleTestProject.obj 1
How do you properly put the implementation in the cpp file?
I saw suggested similar questions but not specific to my error and problem. Here's a simple code on how to recreate the bug. For simplicity, i did not include the getter and setter of the field.
myclass.h
#pragma once
#ifndef myclass_h
#define myclass_h
template <class Type>
class myclass
{
private:
Type value;
public:
myclass(Type value);
};
#endif
myclass.cpp
#include "myclass.h"
template <class Type>
tree<Type>::myclass(Type value)
{
this->value = value;
}
mysample.cpp
#include <iostream>
#include "myclass.h"
int main()
{
myclass <int> aclass(10);
return 0;
}