1

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;
}
winux
  • 452
  • 4
  • 12
  • I added in my question that i'm not asking the same duplicate question. In fact, i believe the opposite. – winux Jul 16 '19 at 06:18
  • You may believe it, but if the answer is "put it in the header because ..." then it is certainly true that **This question already has an answer here:**. – StoryTeller - Unslander Monica Jul 16 '19 at 06:21
  • Why do you think it's not the same as the duplicate? Duplicate explains exactly your error. TL;DR of the dupe: **You cannot put definition of template functions in source file**, because compiler needs to see the full definition of template wherever it's used. – Yksisarvinen Jul 16 '19 at 06:22
  • Notes: You may find the text version of the error messages in the Output Tab easier to wrangle into Stack overflow questions. Plus sometimes they cntain extra information. Very handy. If you know what the template will be used with, you can play around with [explicit template specialization](https://en.cppreference.com/w/cpp/language/template_specialization) – user4581301 Jul 16 '19 at 06:22
  • Nope, as i've mentioned, i'm looking for answers that puts the implementation of a template in the .cpp file. – winux Jul 16 '19 at 06:23
  • It's also explained in the duplicate question, but it may not be what you're looking for. If you put implementation in cpp file, you have to `#include` that file as well. Or use *explicit template specialization*, as linked above by user4581301, but this limits your `template` only to classes you explicitly mention in a cpp file. – Yksisarvinen Jul 16 '19 at 06:26
  • That's what i did, I included the header of that template class definition. check my sample code. – winux Jul 16 '19 at 06:27
  • Please, read the answers in the duplicate. They explain the matter much better than we can do in comments. If you put definition of template fuction in a cpp file, you have to include **the cpp file as well**. Compiler **has to** see the full definition of template functions to compile code. – Yksisarvinen Jul 16 '19 at 06:30
  • 1
    Sadly sometimes the answer to a question is, "You can't do it." At least not for the general case. The duplicate link explains why. – user4581301 Jul 16 '19 at 06:36
  • @Yksisarvinen, surprisingly, your suggestion "include the cpp file as well" worked! but including the .cpp at the end of the header as suggested in the duplicate question link doesn't. I didn't saw any explanation why this works. thanks anyway – winux Jul 16 '19 at 06:44
  • That works for now. But watch what happens later when the cpp file gets more complicated and the [One Definition Rule](https://en.cppreference.com/w/cpp/language/definition) kicks in. – user4581301 Jul 16 '19 at 06:48
  • @user4581301 i know what you mean, and the only solution is to put everything together in the header when its a template method. I think this is a not so clean c++ semantics and does not follow common principles seperating complex definitions in another file. I hope this can be improved to be more consistent to c++ programming principles – winux Jul 16 '19 at 06:51

0 Answers0