Adding data to linked list

I am trying to add data to a linked list. However when I go to add the data I get an error: "no suitable user-defined conversion from "std::string" to "const Ailment" exits. Am I missing something in my push_back function, are my parameters wrong? I don't know what I am doing wrong.

Below is my linkedlist class as well as my main that shows how i am trying to add the data.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
 #pragma once

template <typename T>
class LinkedList
{
public:
	struct Node
	{
		Node* previous;
		Node* next;

		T data;
	};

protected:
	Node* begin_;
	Node* end_;

public:
	LinkedList() : begin_(nullptr), end_(nullptr) {}
	~LinkedList()
	{
		Node* node = begin();
		while(node)
		{
			Node* next = node->next;
			delete node;
			node = next;
		}
	}

	Node* begin() { return begin_; }
	Node const* cbegin() const { return begin_; }
	Node* end() { return end_; }

	void push_front(const T& item)
	{
		Node* node_front = new Node();
		node_front->data = item;

		//check if there is a node to link in-front of
		if (begin_ != nullptr)
		{
			begin_->previous = node_front;
			node_front->next = begin_;
		}
		else
		{
			end_ = node_front;
		}

		begin_ = node_front;
	}

	static void push_back(const T& item)
	{
		Node* node_back = new Node();
		node_back->data = item;

		if (begin_ == nullptr && end_ == nullptr)
		{
			begin_ = node_back;
			end_ = node_back;
			return;
		}

		node_back->previous = end_;
		end_->next = node_back;

		end_ = node_back;
	}

	T pop_front()
	{
		if (begin_ == nullptr)
		{
			//todo: throw
			throw "";
		}

		Node* node = begin_;
		const T value = node->data;
		begin_ = node->next;

		return value;
	}

	T pop_back()
	{
		if (end_ == nullptr)
		{
			//todo: throw
			throw "";
		}

		Node* node = end_;
		const T value = node->data;
		end_ = node->previous;

		return value;
	}

	bool empty() const
	{
		return begin_ == nullptr;
	}

	size_t size() const
	{
		size_t counter = 0;

		Node* node = begin_;
		while (node != nullptr)
		{
			++counter;
			node = node->next;
		}

		return counter;
	}
};


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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
PriorityQueue<Patient> queue;
LinkedList<Ailment>ailments_;

void flush_buffer()
{
	while (getchar() != '\n');
}

int main(int argc, char* argv[])
{
	int user_selection = 10;
	std::string name;
	std::string ailment;
	int severity = 0;
	int time_criticality = 0;
	int contagiousness = 0;

	while (user_selection)
	{
		std::cout << "\nPlease Make A Selection:" << std::endl;

		std::cout << "1 - Add Patient" << std::endl;
		std::cout << "2 - Process Next Patient In Queue" << std::endl;
		std::cout << "3 - Display Queue" << std::endl;
		std::cout << "4 - View Processed Patients History" << std::endl;
		std::cout << "5 - Load Queue" << std::endl;
		std::cout << "6 - Save Queue" << std::endl;
		std::cout << "0 - Exit" << std::endl;
		std::cout << "\nSelection: ";
		std::cin >> user_selection;

		if(user_selection == 0)
		{
			EXIT_SUCCESS;
		}

		flush_buffer();

		switch(user_selection)
		{
		case 1:
			std::cout << "Enter patient name: ";
			std::getline(std::cin, name);

			do
			{
				std::cout << "\tEnter ailment (leave blank when done): ";
				std::getline(std::cin, ailment);
				if (ailment.empty())
				{
					break;
				}

				std::cout << "\tEnter severity: ";
				std::cin >> severity;

				std::cout << "\tEnter time criticality: ";
				std::cin >> time_criticality;

				std::cout << "\tEnter contagiousness: ";
				std::cin >> contagiousness;

				LinkedList<Ailment>::push_back(ailment);
				flush_buffer();
				

			} while (!ailment.empty());

			break;

		case 2:
			std::cout << " moved to patient room!" << std::endl;
			// Process patient with highest priority
			break;

		case 3:
			std::cout << "Patients In Queue:" << std::endl;
			// Display patient queue
			break;

		case 4:
			std::cout << "History:" << std::endl;
			// Display history of processed patients
			break;
	
		case 5:
			std::cout << "Enter path to file: ";
			// Load a .csv file
			break;

		case 6:
			std::cout << "Enter path to file: ";
			// Save to .csv file
			break;

		case 0:
			EXIT_SUCCESS;
		}
	}
}
Last edited on
Line 2: You haven't shown the definition of Ailment. Presumably, it is a class.

Line 63: You're trying to do a push_back() of a std::string to a LinkedList of Ailments (whatever that is).

Line 63: You're doing a push_back() to an unnamed object. Line 63 goes out of scope at line 67. Line 63 should be:
1
2
    Ailment temp(ailment);  //  Create temporary object from user input
    ailments_.push_back(temp);


Line 34,97: EXIT_SUCCESSS expands to the value 0. It does NOT call exit().

Lines 42-68: You should consider placing this code in it's own function. By the time you flush out the code in the switch statement, it's going to get unwieldly.

Line 34,97: You should avoid calling exit(). exit() does not clean up in the same way that a simple return 0; does.

edit: regarding Ailment: Sorry. Had not seen your enqueue/dequeue thread.
However, you should still post all relevant classes for your question.

edit #2: Regarding line 63. I just saw the static on your push_back() function. You don't want to do this. This will operate as if you have only one linked list of ailments. That may be true for the moment, but as Jonin pointed in the enqueue/dequeue thread, you might want want a linked list of ailments per patient.
Last edited on
Topic archived. No new replies allowed.