finding items in generic list

I have a template list class of template nodes and I'm trying to put all the functionality in a separate iterator class (for learning) - but the below is getting an error:

error C2280: 'node<T>::node(const node<T> &)': attempting to reference a deleted function which I don't really understand, are there any glaring errors below?

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
#include "jlist.h"
#include "node.h"

template <typename T>
class jlist_iterator {
private:
	std::shared_ptr<node<T>> iter{ nullptr };
	std::shared_ptr<node<T>> begin{ nullptr };
public:
	jlist_iterator(jlist<T>& list)
	{
		if (list.head == nullptr)
		{
			std::cout << "\nIterator tried to initialize with nullptr\n"; 
			return;
		}
		iter = std::make_shared<node<T>>() = list.head;
		begin = std::make_shared<node<T>>() = list.head;
	}
	~jlist_iterator() { iter.reset(); }
	node<T> find(T item)
	{
		iter = begin;
		if (*iter->data == item)
		{
			std::cout << "\n\nitem found\n\n";
			return *iter;
		}
		while (iter->next != nullptr)
		{
			iter = iter->next;
			if (*iter->data == item)
			{
				std::cout << "\n\nitem found\n\n";
				return *iter;
			}
		}
		std::cout << "\n\nItem NOT found\n\n";
	}
	bool operator == (T item)
	{
		return (*this->data != item);
	}
};
You're not showing enough content, and it appears that you're probably not showing all the error messages. The message is talking about something called a node, but you failed to show anything to do with a node. Also note that when working with templates you need to show how those templates are being used, since templates are not created until actually used.

Did you look up the documentation for that compiler error message?

But to answer part of your unasked question there is a mistake in that function, it fails to return a value for all execution blocks.

Thanks jlb, do you have a link for a good place to look at compiler errors? My searches bring up stackoverflow, i'm using VS 2017

The rest of the code is below. It all works fine, until I try to find something in a list with code like this:

jlist<std::string> test;
test.add("Ice Cream");
jlist_iterator<std::string> it(test);
it.find("Ice Cream");

so the problem must be in the node<T> find() function I think, the only other error message is:
node<T>::node(const node<T> &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::unique_ptr<T,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)'

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
//node.hpp

#include <memory>

template <typename T>
class node {
public:
	std::unique_ptr<T> data;
	std::shared_ptr<node> next;
	bool operator = (std::shared_ptr<node<T>> n)
	{
		this->next = n;
	}
	bool operator == (std::shared_ptr<node<T>> n)
	{
		return (this->data != nullptr);
	}
};

//jlist.hpp

template <typename T>
class jlist {
public:
	std::shared_ptr<node<T>> head{ nullptr };
	jlist() = default;
	~jlist() = default;
	void add(T item)
	{
		if (head == nullptr)
		{
			head = std::make_shared<node<T>>();
			head->data = std::make_unique<T>(item);
			head->next = nullptr;
		}
		else
		{
			std::shared_ptr<node<T>> iter = head;
			while (iter->next != nullptr)
			{
				iter = iter->next;
			}
			std::shared_ptr<node<T>> temp = std::make_shared<node<T>>();
			temp->data = std::make_unique<T>(item);
			iter->next = temp;
		}
	}
	void print()
	{
		if (head == nullptr)
			return;
		std::shared_ptr<node<T>> iter = head;
		std::cout << *iter->data << std::endl;
		while (iter->next != nullptr)
		{
			iter = iter->next;
			std::cout << *iter->data << std::endl;
		}
	}
	friend std::ostream& operator << (const std::ostream& os, const T& item)
	{
		return os << item;
	}
};
do you have a link for a good place to look at compiler errors?

Try searching for the error number, the first several MSDN links should be good choices.

The rest of the code is below. It all works fine, until I try to find something in a list with code like this:

That is not the "rest" of the code. You need to show exactly how you're trying to use those templates. Usually the smallest possible complete program that illustrates the problem.

What line exactly is causing the problem? Your error message should be telling you that information.

Again post the complete error messages, all of them (this includes any warning messages as well), exactly as they appear in your development environment and be sure to post the actual code that caused those errors.

Also since that error message mentions unique_ptr, you may want investigate if you're somehow trying to copy that pointer.
iter = std::make_shared<node<T>>() = list.head;
¿what? ¿why there are two assignments?

node<T> find(T item)
you can't return a `node' because you can't copy (or assign) a `node'
you can't copy a `node' because it has a `unique_ptr' as a member
Topic archived. No new replies allowed.