C++ Error : undefined reference to `Vector2<int>::add(int)'

C++ Error : undefined reference to `Vector2<int>::add(int)'

我是 C++ 新手

我一直在尝试在下面编写一个链表,你可以看到我有 linkedList header 并实现了一个主文件,我正在使用 MinGW-W64 作为编译器

在我的 linkedList.h 中,我有模板 struct Node 和模板 class Vector2

// linkedList.h
template <class T>
struct Node
{
    T value;
    Node *next;
};

template <class T>
class Vector2
{
private:
    Node<T> root;

public:
    void add(T value);
    ~Vector2();
};

在我的 linkedList.cpp 中,我实现了 void Vector2<T>::add(T value);Vector2<T>::~Vector2();

// linkedList.cpp
#include <iostream>
#include "linkedList.h"

using namespace std;

template <class T>
void Vector2<T>::add(T value)
{
    Node<T> *newNode = new Node<T>;
    newNode->value = value;

    if (root == nullptr)
    {
        newNode->value = nullptr;
        root = newNode;
        return;
    }

    newNode->next = root;
    root = newNode;
}

template <class T>
Vector2<T>::~Vector2()
{
}

在 main 中我创建了一个 Vector2 vec;

// main.cpp
#include <iostream>
#include "linkedList.h"

using namespace std;

int main()
{
    Vector2<int> vec;
    vec.add(12);
    return 0;
}

当我编译代码时出现此错误

$> make
g++ -g -Wall -o build\main main.cpp linkedList.cpp && echo Executing build\main && build\main.exe
\AppData\Local\Temp\ccb8gQgf.o: In function `main':
\Desktop\github\c\templates/main.cpp:9: undefined reference to `Vector2<int>::add(int)'
\Desktop\github\c\templates/main.cpp:10: undefined reference to `Vector2<int>::add(int)'
\Desktop\github\c\templates/main.cpp:11: undefined reference to `Vector2<int>::add(int)'
\Desktop\github\c\templates/main.cpp:8: undefined reference to `Vector2<int>::~Vector2()'
\Desktop\github\c\templates/main.cpp:8: undefined reference to `Vector2<int>::~Vector2()'
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:13: main] Error 1

模板函数不能分割成多个文件。您必须在头文件中实现它。