0

I'm new to C++

I have been attempting to write a linked list below you can see I have linkedList header and implement and a main file I'm using MinGW-W64 as compiler

in my linkedList.h I have template struct Node and a template 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();
};

in my linkedList.cpp I implemented void Vector2<T>::add(T value); and 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()
{
}

in main I created a Vector2 vec;

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

using namespace std;

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

when I compile my code I get this errors

$> 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
Amine Beihaqi
  • 177
  • 2
  • 8

1 Answers1

1

Template functions cannot be diveded as multiple files. You must inplement it in your header file.

heartleth
  • 132
  • 7
  • 2
    If there is a duplicate, that is more detailed than your answer: you should flag as a duplicate, instead of providing that answer. – Algirdas Preidžius Apr 09 '21 at 14:03
  • 1
    @AlgirdasPreidžius This is a meta-question, but really, should the answer be "punished" for the "mistake" the OP made? This answer is the right one, albeit lacking details. – Gotiasits Apr 09 '21 at 14:07
  • @AlgirdasPreidžius This is one of the most common duplicates on SO. On the other hand, the person answering the question seems to have just started answering questions here and don't necessarily know that. A good answer gets my upvote _and_ I'll close the question if it's a dupe. I don't see a reason to discourage people from answering just because they don't know the full history of all the answers on SO. I won't upvote this one because it's a little bit wrong. It's fine to place the implementation in a `.cpp` file if your template is designed to handle a known set of types only. – Ted Lyngmo Apr 09 '21 at 15:47