0

I have a problem with linker. I am trying to create template Tree but since I'm having some problems with using templates (my first time) I decided to do a "test template program". I have been looking for anserws before posting and tried some solutions posted on the forum but none has worked for me. Here is the test program:

.h file

#pragma once
template <class T>
class Node {
public:
    Node();
    Node(T value);
    ~Node();
    void manualTree();
private:
    T data;
    Node *left, *right, *parent;
};

.cpp file

#include "stdafx.h"
#include <iostream>
#include "Node.h"
using namespace std;

template<class T>
Node<T>::Node(T val) {
    data = val;
}

template<class T>
void Node<T>::manualTree() {
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    cout << "left : " << root->left->data << endl;
    cout << "right : " << root->right->data << endl;
    cout << "root : " << root->data << endl;

}

template class Node<int>;

main body

#include "stdafx.h"
#include "arithmetics.h"
#include <iostream>
#include "Node.h"
using namespace std;

int main()
{
    Node<int> n;
    n.manualTree();
    return 0;
}

Here is what i get after trying to call manualTree() function: Severity Code Description Project File Line Column Suppression State

Error   LNK1120 2 unresolved externals  zad6 - szablony C:\Users\Piotr\source\repos\zad6 - szablony\Debug\zad6 - szablony.exe   1   1   
Error   LNK2019 unresolved external symbol "public: __thiscall Node<int>::~Node<int>(void)" (??1?$Node@H@@QAE@XZ) referenced in function _main  zad6 - szablony C:\Users\Piotr\source\repos\zad6 - szablony\zad6 - szablony\main.obj    1   1   
Error   LNK2019 unresolved external symbol "public: __thiscall Node<int>::Node<int>(void)" (??0?$Node@H@@QAE@XZ) referenced in function _main   zad6 - szablony C:\Users\Piotr\source\repos\zad6 - szablony\zad6 - szablony\main.obj    1   1   

EDIT:

I have moved everything to the .h file, like they said in the linked post and i still get the same warnings sometimes with a few more. Already tried putting into .cpp file these lines and still not working (with the same warnings):

template class Node<int>;

or

Node<int> n;

new .h file

#pragma once
template <class T>
class Node {
public:
    Node();
    Node(T value) {
        data = value;
    }
    ~Node();
    void manualTree() {
        Node* root = new Node(1);
        root->left = new Node(2);
        root->right = new Node(3);
        cout << "left : " << root->left->data << endl;
        cout << "right : " << root->right->data << endl;
        cout << "root : " << root->data << endl;

    }
private:
    T data;
    Node *left, *right, *parent;
};
hdw3
  • 871
  • 10
  • 28
  • Implementing your member functions in the header file is simplest solution. Alternately you can explicitly export particular template instantiations – Joe Jan 07 '18 at 20:14
  • after moving implementation to the header file i get exactly same message with one more linker problem as a bonus. By implementing in header file, you mean not even creating .cpp file and doing everything in .h file, right? – hdw3 Jan 07 '18 at 20:23
  • Templates are not like java generics. They need access to the source code during compile time. Unless you only want to explicitly instantiate a few types its best to put all code (for template objects/functions) in header files. Note there are workarounds to this but unless you really want to ensure your code is not public, its generally not worth it. – Joseph Franciscus Jan 07 '18 at 21:23
  • I moved the whole implementation to .h file and implementation looks like above at "new .h file section" and I am still having the same problems with linker. I don't know what i am doing wrong at this point. – hdw3 Jan 07 '18 at 21:29
  • 1
    Read your errors they specifically say you do not have definitions of your default constructor `Node();` and destructor `~Node();` – Joseph Franciscus Jan 07 '18 at 21:31
  • Wow, so this is, what those messages meant. Thanks a lot ;) – hdw3 Jan 07 '18 at 21:33
  • No problem, if you call manualTree() make sure you create a destructor that can handle deleting the nodes you created otherwise you create a memory leak. – Joseph Franciscus Jan 07 '18 at 21:35
  • Also I am assuming you are using VisualStudio (because of #pragma once) I *highly* recommend disabling language extensions. Language extensions in Visual Studio allow you to create code that is not C++ standard (and it comes to bite you in the but if you ever switch to writing in a linux enviroment (which I also recommend)) – Joseph Franciscus Jan 07 '18 at 21:37
  • Okay, thanks for the advice, it is nice to learn something ;) – hdw3 Jan 07 '18 at 21:42

0 Answers0