Why am I receiving this compiler error? g++ -g -Wall -Wshadow -Wuninitialized -O
ID: 3937776 • Letter: W
Question
Why am I receiving this compiler error?
g++ -g -Wall -Wshadow -Wuninitialized -O -c pqueue.cpp
pqueue.cpp: In function ‘void remove_item(event*&, PQPriorityType&, PriorityQueue&)’:
pqueue.cpp:97:49: error: ‘memcpy’ was not declared in this scope
memcpy(item, curr->item, sizeof(*curr->item));
^
makefile:16: recipe for target 'pqueue.o' failed
make: *** [pqueue.o] Error 1
Code - - - - - - - - -- - - - - - - - - - -- - - - -
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cassert>
#include "pqueue.h"
using namespace std;
// Constructor for PriorityQueue.
PriorityQueue::PriorityQueue() {
head = NULL;
}
// Constructor for a PQCell.
PQCell::PQCell(PQItemType item_, PQPriorityType pri_, PQCell * next_){
item = item_;
priority = pri_;
next = next_;
}
// Checks if the list is empty or not.
bool isEmpty(const PriorityQueue &q) {
return (q.head == NULL);
}
//insert a new Node to the back of the list
void insert(PQItemType item, PQPriorityType pri, PriorityQueue& q) {
// 1. make a new PQCell
// 2. check if the list is empty, if it is. assign head of the list
// to new node and return
PQCell * newCell = new PQCell(item, pri, NULL);
if(isEmpty(q)) {
q.head = newCell;
return;
}
PQCell * insert_at = q.head;
while(insert_at->next != NULL) {
insert_at = insert_at->next;
}
// we have to insert at insert_at
insert_at->next = newCell;
}
// Removes the item with lowest prioirty and put the values
// in item and pri.
void remove_item(PQItemType& item, PQPriorityType& pri, PriorityQueue& q) {
if(isEmpty(q)) {
return; //nothing in list, return
}
PQCell * prev = NULL;
PQCell * curr = q.head;
PQPriorityType smallest_priority = q.head->priority;
while(curr != NULL) {
if(curr->priority < smallest_priority) {
smallest_priority = curr->priority;
}
curr = curr->next;
}
prev = NULL;
curr = q.head;
while(curr->priority > smallest_priority) {
prev = curr;
curr = curr->next;
}
PQCell *nextNode = curr->next;
if(prev != NULL) {
prev->next = nextNode;
} else {
q.head = nextNode;
}
memcpy(item, curr->item, sizeof(*curr->item));
pri = curr->priority;
delete curr;
}
// Prints the list.
void printPriorityQueue(const PriorityQueue& q, ItemPrinter pi, PriorityPrinter pp){
PQCell * head = q.head;
while(head != NULL) {
pi(head->item);
pp(head->priority);
printf(" ");
head = head->next;
}
}
Explanation / Answer
Try this one :
#include<iostream.h>
#include<conio.h>
const int MAX=5;
class pqueue
{
int front,rear;
public:
struct data
{
int val,p,o;
}d[MAX];
pqueue()
{
front=rear=-1;
}
void insert(data d1);
data deletion();
void display();
};
void pqueue :: insert(data d1)
{
if(rear==MAX-1)
cout<<"Priority Queue is Full
";
else
{
rear++;
d[rear]=d1;
if(front==-1)
front=0;
data temp;
for(int i=front;i<=rear;i++)
for(int j=i+1;j<=rear;j++)
{
if(d[i].p > d[j].p)
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}
else
{
if(d[i].p==d[j].p)
{
if(d[i].o > d[j].o)
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}
}
}
}
}
}
data pqueue :: deletion()
{
data d1;
if(front==-1)
cout<<"Priority Queue is Empty
";
else
{
d1=d[front];
if(front==rear)
front=rear=-1;
else
front++;
}
return d1;
}
void pqueue :: display()
{
if(front==-1)
cout<<"Priority Queue is Empty
";
else
{
for(int i=front;i<=rear;i++)
{
cout<<"Object :"<<i+1<<endl;
cout<<"Value ="<<d[i].val<<endl;
cout<<"Priority="<<d[i].p<<endl;
cout<<"Order = "<<d[i].o<<endl;
}
}
}
void main()
{
pqueue p1;
data d1;
char op;
do
{
int ch;
clrscr();
cout<<"----------Menu-------------
";
cout<<"1.Insertion
2.Deletion
3.Display
4.Exit
";
cout<<"Enter your Choice<1..4> ?";
cin>>ch;
switch(ch)
{
case 1 : cout<<"Enter Value ?";
cin>>d1.val;
cout<<"Enter Priority?";
cin>>d1.p;
cout<<"Enter Order ?";
cin>>d1.o;
p1.insert(d1);
break;
case 2 : d1=p1.deletion();
cout<<"Value = "<<d1.val<<endl;
cout<<"Priority = "<<d1.p<<endl;
cout<<"Order ="<<d1.o<<endl;
break;
case 3 : p1.display();
break;
}
cout<<"Do You Want to Continue <Y/N> ?";
cin>>op;
}while(op=='Y' || op=='y');
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.