I would like to be able to build a class using a generic type (template in c ++), I checked the documentation and some threads, and I do not have the impression to have made mistakes, yet I have an error when I try to compile (unresolved external symbol ...). I tried to change the keyword "typename" by the keyword "class", but that does not change anything

PS: The Console application (for tests) and the NodeEditor project are two different projects, I generate a lib (NodeEditor side) and use it in the console application

EDIT (solved) :

"Generic template definitions cannot be in an independent compilation unit (.cpp file). See Storing C++ template function definitions in a .CPP file."

NodeLinker.h

#ifndef _NODELINKER_H_
#define _NODELINKER_H_

#include <type_traits>

namespace NodeEditor
{
    template <typename T>
    class NodeLinker
    {
        public:
            NodeLinker();
            ~NodeLinker();
    };
}
#endif

NodeLinker.cpp

#include "stdafx.h"
#include "NodeLinker.h"

#include <iostream>

using namespace NodeEditor;

template<typename T>
NodeLinker<T>::NodeLinker()
{
    std::cout << "Node Linker Constructed" << std::endl;
}

template<typename T>
NodeLinker<T>::~NodeLinker()
{
    std::cout << "Node Linker Destructed" << std::endl;
}

NodeEditorTestConsole.cpp

#include "pch.h"

#include <iostream>
#include "NodeLinker.h"

using namespace NodeEditor;

int main()
{
    //Test 1 (not working) :
    NodeLinker<int> *nodeLinker = new NodeLinker<int>();

    //Test 2 (not working) :
    NodeLinker<int> nodeLinker;

    //Test3 (not working) :
    NodeLinker <int> nodeLinker = NodeLinker<int>();

    return 0;
}

Thank you in advance for your help, and have a nice day!

0 Answers