0

I have pretty much the same issue as unresolved external symbol "public: __thiscall, but since I've defined all my methods, the solution there doesn't apply here.

I have a Node.h with:

template<typename T>
class Node{
    public:
        Node(T data, Node* next);
}

In Node.cpp, I was sure to put:

#include "Node.h"

template<typename T>
Node<T>::Node(T data, Node* next){
    //I doubt that the implementation is relevant.
}

In both files, I #include "stdafx.h" to cater to visual studios.

Yet, when I try to instantiate a node with:

#include "stdafx.h"
#include "Node.h"

int main(){
    Node<int> n = Node<int>(9, nullptr);
    return 0;
}

I get:

main.obj : error LNK2019: unresolved external symbol "public: __thiscall Node<int>::Node<int>(int,class Node<int> *)" (??0?$Node@H@@QAE@HPAV0@@Z) referenced in function _main

Just to debunk a few initial theories:

  • I have put all the files in the same VC++ project.
  • There are no methods declared in the header that are not defined (VC++ has ensured this).
  • I instantiate the node in main.cpp, hence the uses of main.obj in the error.

Is there another issue that I don't see (maybe a Node.h crawling the standard library or some VC++ thing I'm missing). Comparing this to older code I have that does work, the only difference I can find is that the old code doesn't use generics. Are the generics the culprit?

Why won't these files link up correctly? How to avoid this in the future?

Community
  • 1
  • 1
Heman Gandhi
  • 1,343
  • 9
  • 12
  • Pretty much the same problem - when the compiler sees just a declaration, it assumed *someone else* has instantiated the template. Bu t *someone else* hasn't done that, so the linker can't find it. – Bo Persson Sep 11 '15 at 12:42
  • Thank you. Sorry to post a duplicate, I just never considered that templates would be so fiendishly complex. I guess I don't know what I don't know. – Heman Gandhi Sep 11 '15 at 12:59

0 Answers0