priority queue exercise

i got error C2061 line 13 syntax error identifer bug
line 68 C2760 unexpected token '.' expected declaration

Imagine you're writing a little program to categorize bugs in a game and you want to push the bugs into a queue. The bugs will have a char priority(level) of 'A', 'B', or 'C'.
I want you to create a small class (or struct) for the bugs and they only have to have two properties:
A char for the priority with a suggested name of m_cPriority
A string or char* for the description with a suggested name of m_sDesc

You'll be creating a queue of these objects as the nodes and this will be somewhat of a custom priority queue exercise.
For the most part, you're going to change the enQueue method of the BasicQueue to account for the priority of the Bug. For maximum templatability(?), you should have the char priority as the second parameter in the new enQueue in case you want to test the queue with other types. Then you would somehow need to set the priority of the node in the main when enQueue is called.
For example, when enQueueing a Bug node, invoke a GetPriority method for the Bug that returns the char priority and have that function invocation as the second parameter of the enQueue call.
Or, if you want to enQueue a simple type like int, give the simple nodes an arbitrary char priority. They would, of course, not be Bugs but you're still able to use this priority queue for other purposes.

So, in the enQueue, you have to check the priority of the Node and then insert the Node in the queue based on these priorities. 'A' have the highest priority, 'B' second and 'C' third. Nodes of the same priority are pushed to the end of their respective priorities. For example a new 'A' bug, will get pushed to the end of the 'A's already in the queue.
All I want you to alter is the enQueue and add the Bug struct and account for this new priority.

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
122
123
124
#pragma once
#include <iostream>

using namespace std;

template <class T>
struct Bug {
	char m_cpriority;
	string m_sDesc;
	T data;
	Bug(const T &);
	Bug(const T &, char);
	char enQueue(bug, Bug.GetPriority());
};

template <class T>
class Node
{
public:
	T m_tData;
	Node<T>* m_pNext;
	Node(T val) : m_tData(val), m_pNext(nullptr) {}
};

template <class T>
class PriorityQueue {
private:
	Node<T> *head; 
public:
	PriorityQueue();
	~PriorityQueue();
	void enQueue(const Bug<T> &);
	Bug<T> deQueue();
	bool isEmpty() const;
	void print() const;
};

template <class T>
class BasicQueue
{
private:
	Node<T>* m_pHead;
	Node<T>* m_cpriority;
public:
	BasicQueue() : m_pHead(nullptr), m_cpriority(nullptr) {}
	~BasicQueue()
	{
		//Node<T>* current = m_pHead; // Consider this an iterator.
		Node<T>* next;
		while (m_pHead != nullptr)
		{
			next = m_pHead->m_pNext;
			delete m_pHead;
			m_pHead = next;
		}
	}
	void enQueue(T val) // 'Push' method.
	{
		Node<T>* newNode = new Node<T>(val);
		if (m_pHead == nullptr) // List is empty.
		{
			m_pHead = m_cpriority = newNode;
		}
		else // New node becomes the new tail.
		{
			m_cpriority->m_pNext = newNode;
			Bug.GetPriority();
			m_cpriority = m_cpriority->m_pNext;
		}
	}
	T deQueue() // 'Pop' method. 
	{
		if (m_pHead != nullptr)
		{
			T val = m_pHead->m_tData; // Storing the data to be returned.
			Node<T>* next;
			next = m_pHead->m_pNext;
			delete m_pHead; // Deallocating head.
			m_pHead = next;
			return val; // Throwing copy of old head's data.
		}
	}
	bool isEmpty()
	{
		return m_pHead == nullptr;
	}
	void print()
	{
		if (isEmpty())
			return;
		cout << "Queue contents: ";
		Node<T>* current = m_pHead;
		while (current != nullptr)
		{
			cout << current->m_tData << ' ';
			current = current->m_pNext;
		}
		cout << endl;
	}
};{/code}

[code] 
#include "basicthing.h"
using namespace std;

int main()
{
	BasicQueue<int> iQ;
	iQ.enQueue(42);
	iQ.enQueue(17);
	iQ.enQueue(7);
	iQ.enQueue(117);
	iQ.print();
	//Unit uOne("Infantry");
	//Unit uTwo("Commando");
	//<Unit> uQ;
	//uQ.enQueue(uOne);
	//uQ.enQueue(uTwo);
	//uQ.print();
	//uQ.deQueue();
	//uQ.print();
	system("pause");
	return 0;
}


i need help to make this work
Last edited on
char enQueue(bug, Bug.GetPriority());

What is bug ? It doesn't exist. You can't use things that don't exist.

This entire line makes no sense. It's a function declaration. bug is not a type. Bug.GetPriority() is not a type.
how do I get to priorities to have A B C
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
#pragma once
#include <iostream>

using namespace std;

template <class T>
struct Bug {
	char m_cpriority;
	string m_sDesc;
	T data;
	Bug(const T &);
	Bug(const T &, char);
   char	GetPriority();
};

template <class T>
class Node
{
public:
	T m_tData;
	Node<T>* m_pNext;
	Node(T val) : m_tData(val), m_pNext(nullptr) {}
};

template <class T>
class PriorityQueue {
private:
	Node<T> *head; 
public:
	PriorityQueue();
	~PriorityQueue();
	void enQueue(const Bug<T> &);
	Bug<T> deQueue();
	bool isEmpty() const;
	void print() const;
};

template <class T>
class BasicQueue
{
private:
	Node<T>* m_pHead;
	Node<T>* m_cpriority;
public:
	BasicQueue() : m_pHead(nullptr), m_cpriority(nullptr) {}
	~BasicQueue()
	{
		//Node<T>* current = m_pHead; // Consider this an iterator.
		Node<T>* next;
		while (m_pHead != nullptr)
		{
			next = m_pHead->m_pNext;
			delete m_pHead;
			m_pHead = next;
		}
	}
	void enQueue(T val) // 'Push' method.
	{
		Node<T>* newNode = new Node<T>(val);
		if (m_pHead == nullptr) // List is empty.
		{
			m_pHead = m_cpriority = newNode;
		}
		else // New node becomes the new tail.
		{
			m_cpriority->m_pNext = newNode;
			
			m_cpriority = m_cpriority->m_pNext;
		}
	}
	T deQueue() // 'Pop' method. 
	{
		if (m_pHead != nullptr)
		{
			T val = m_pHead->m_tData; // Storing the data to be returned.
			Node<T>* next;
			next = m_pHead->m_pNext;
			delete m_pHead; // Deallocating head.
			m_pHead = next;
			return val; // Throwing copy of old head's data.
		}
	}
	bool isEmpty()
	{
		return m_pHead == nullptr;
	}
	void print()
	{
		if (isEmpty())
			return;
		cout << "Queue contents: ";
		Node<T>* current = m_pHead;
		while (current != nullptr)
		{
			cout << current->m_tData << ' ';
			current = current->m_pNext;
		}
		cout << endl;
	}
}; 
how do I get to priorities to have A B C


Set the value of m_cpriority in each Bug object you create.

Right now, you don't seem to actually create any Bug objects.
how do I create a Bug object
You have specified that the Bug object has these constructors:
1
2
Bug(const T &);
Bug(const T &, char);


So you can create them, for example, like this:

1
2
Bug<int> x(7);
Bug<string> y ("Beans", C);


Don't forget to implement those constructors.

Last edited on
Topic archived. No new replies allowed.