struct within class template - linked list

Hello, I am making a linked list and using a class template, and within the class I have a struct for the node. But I am having problems creating the functions of the linked list in the .cpp . I think the problems may be coming from where to have template<typename T> but i am not sure. The issue I am getting is with the insert in the cpp, it says that the declaration is incompatible with the function template, but I do not know why? This error does go away if i take out the list:: part, but then nothing within the function is recognised. And I have always used the scope resolution operator to link the functions, so assume that should always be there?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  header file

#pragma once

template<typename T>
class list
{
private:
	struct Node
	{
		list<T> data;
		Node* next;		
	};

public:
	list() {};
	~list();


	Node* head;

	//insert
	//search
	//delete
	//read - print

	template<typename T>
	void insert(int pos, list<T> data);




};


cpp

#include "list.h"

template<typename T>
void list<T>::insert(int pos, list<T> data)
{
	Node newNode = new Node();
	newNode->data = data;

	if (pos == 0)
	{
		newNode->next = head;
		head = newNode;
		return;
	}

	Node* tempNode;
	tempNode = head;
	for (int i = 0; i < pos - 1; i++)
	{
		tempNode = tempNode->next;
	}
	Node temp2 = new Node();
	temp2->next = tempNode->next;
	tempNode->next = temp2;
	return;
}



Lines 28,41: You're passing the list by value which means the compiler is making a shallow copy. Any changes to the list are lost on exist from insert(). You need to pass the list by reference.
Lines 28,41: You're passing the list by value which means the compiler is making a shallow copy. Any changes to the list are lost on exist from insert(). You need to pass the list by reference.


ok, I am doing something definitely wrong then as I dont want to be passing the list, I thought list<T> data, would just indicate that the data is of type whatever T is in list. Unless you are meaning somewhere else?
Maybe you just want to pass const T& data
I see a class called list which contains a struct called node which contains a class list called data... What? I get that same kind of feeling when I hold a mirror up in front of another mirror, and create an infinite reflection.
EDIT : I have fixed this issue, but now seem to be getting a linking error for the function

Error LNK2019 unresolved external symbol "public: void __thiscall list<int>::insert(int,int const &)" (?insert@?$list@H@@QAEXHABH@Z) referenced in function _main



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
header

#pragma once

template<typename T>
class list
{
private:
	struct Node
	{
		T data;
		Node* next;

		Node() = default;
		Node(Node* Next, const T& Data) : next(Next), data(Data) {}
	};

public:
	list() = default;
	//~list();

	Node* head = nullptr;

	//insert
	//search
	//delete
	//read - print

	
	void insert(int pos, const T& data);

};



cpp

#include "list.h"

template<typename T>
void list<T>::insert(int pos, const T& data)
{
	Node* newNode = new Node();
	newNode->data = data;

	if (pos == 0)
	{
		newNode->next = head;
		head = newNode;
		return;
	}

	Node* tempNode;
	tempNode = head;
	for (int i = 0; i < pos - 1; i++)
	{
		tempNode = tempNode->next;
	}
	Node* temp2 = new Node();
	temp2->next = tempNode->next;
	tempNode->next = temp2;
	return;
}



main

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

int main()
{
	list<int> newList{};

	newList.insert(0, 1);
	newList.insert(1, 3);
	newList.insert(2, 5);

	


}

Last edited on
1
2
3
4
template<typename T>
void list<T>::insert(int pos, const T& data)
{
...

Last edited on

template<typename T>
void list<T>::insert(int pos, const T& data)
{
..


Sorry, I thought I had updated the post with the upto date code, but I must have missed a part. I had fixed the issues I was having by doing the same as what you have shown, and thats when I started to get the linking error.
I'm not sure what the problem is. This seems to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
template<typename T>
class list
{
private:
    struct Node
    {
        T data;
        Node* next;

        Node(const T& data, Node* next = nullptr) : data(data), next(next) {}
    };

    Node* head = nullptr;

public:
    list() = default;
    ~list();
    void insert(int pos, const T& data);
    void print() const;
};

#include <iostream>

// Insert data before position pos.
// A negative position is equivalent to 0.
// Positions past the end place data at end.
template<typename T>
void list<T>::insert(int pos, const T& data)
{
    Node *nd = head, *prev = nullptr;

    for (int i = 0; i < pos && nd; ++i)
    {
        prev = nd;
        nd = nd->next;
    }

    if (prev)
        prev->next = new Node(data, prev->next);
    else
        head = new Node(data, head);
}

template<typename T>
void list<T>::print() const
{
    for (Node* nd = head; nd; nd = nd->next)
        std::cout << nd->data << ' ';
    std::cout << '\n';
}

template<typename T>
list<T>::~list()
{
    for (Node* nd = head; nd; )
    {
        Node* del = nd;
        nd = nd->next;
        delete del;
    }
}

int main()
{
    list<int> a;

    a.insert(0, 1);
    a.insert(1, 3);
    a.insert(2, 5);

    a.insert(99, 7); // if position is past the end it's put at the end
    a.insert(-99, 0); // negative position equivalent to position 0

    a.insert(0, -1);
    a.insert(3, 2);
    a.insert(5, 4);
    a.insert(7, 6);

    a.print();
}

OP's mistake seems to be that the template is implemented in a separate translation unit.
https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
Last edited on
Topic archived. No new replies allowed.