I created a structure called leafNode which contains two data fields at this point (an int and a char), and would like to store it in a priority queue but am getting build errors.
For some reason you are creating leafNode pointers when you don't need to. You are also writing to the pointers without allocating any memory for them which can cause all kinds of trouble.
Also you have not provided an ordering function that the priority_queue can use to establish which leafNode has priority over the other.
#include <iostream>
#include <queue>
usingnamespace std;
struct leafNode
{
char character;
int count;
// Need a comparison function to establish the priority
booloperator<(const leafNode& n) const
{
return character < n.character; // order according to character code?
}
}; // *p; no need for global pointer
void testRead()
{
priority_queue<leafNode> myqueue;
char letter;
leafNode p; // creat your leafNode locally (and not a pointer)
p.character = '~';
p.count = 0;
for(int i = 0; i <= 5; i++)
{
cout << "type a single character" << endl;
cin >> letter;
p.character = letter;
p.count++;
myqueue.push(p);
}
while(!myqueue.empty())
{
const leafNode& q = myqueue.top();
cout << q.character << " " << q.count << endl;
myqueue.pop();
}
}
int main()
{
testRead();
return 0;
}
type a single character
w
type a single character
r
type a single character
s
type a single character
g
type a single character
d
type a single character
j
w 1
s 3
r 2
j 6
g 4
d 5
Thank you so much for that Galik, and thank you for answering so quickly. That definitely solved the issue I was having. I was slowly arriving at this conclusion when you answered but still didn't know how to set the priority field. Thank you again.