I copied and edited this code to \"work\" but it comes up with thisodd linking e
ID: 3608972 • Letter: I
Question
I copied and edited this code to "work" but it comes up with thisodd linking error. I can't for the life of me figure outwhats wrong.Thanks in advance
/tmp/ccziZ23i.o: In function `Queue::Queue()':
Queue.cpp:(.text+0x0): multiple definition of `Queue::Queue()'
........
Queue.cpp:(.text+0x186): multiple definition of`Queue::enqueue(int)'
/tmp/ccAAOFkz.o:testQueue.cpp:(.text+0x1be): first defined here
collect2: ld returned 1 exit status
#include <iostream>
using namespace std;
#include "Queue.h"
Queue:: Queue()
{
front = NULL;
rear = NULL;
numItems =0;
}
//********************************************
//Destructor *
//********************************************
Queue::~Queue()
{
clear();
}
//********************************************
// Function enqueue inserts the value in num *
// at the rear of thequeue. *
//********************************************
void Queue::enqueue(int num)
{
Node *newNode;
// Create a new node and store num there.
newNode = new Node;
newNode->value = num;
newNode->next = NULL;
// Adjust front and rear as necessary.
if (isEmpty())
{
front = newNode;
rear = newNode;
}
else
{
rear->next = newNode;
rear = newNode;
}
// Update numItems.
numItems++;
}
//**********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies it into num. *
//**********************************************
int Queue::dequeue()
{
Node *temp;
int num;
if (isEmpty())
{
cout << "The queue isempty. ";
return 0;
}
else
{
// Save the front nodevalue in num.
num = front->value;
// Remove the front nodeand delete it.
temp = front;
front =front->next;
delete temp;
// Update numItems.
numItems--;
}
return num;
}
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and falseotherwise. *
//*********************************************
bool Queue::isEmpty() const
{
bool status;
if (numItems > 0)
status = false;
else
status = true;
return status;
}
//********************************************
// Function clear dequeues all the elements *
// in thequeue. *
//********************************************
void Queue::clear()
{
while(!isEmpty())
dequeue();
}
#include <iostream>
using namespace std;
class Queue
{
private:
struct Node
{
int value;
Node *next;
};
Node *front;
Node *rear;
int numItems;
public:
Queue();
~Queue();
void clear();
bool isEmpty() const;
int dequeue();
void enqueue(int);
bool isFull() const;
};
#include <iostream>
using namespace std;
#include "Queue.cpp"
int main()
{
Queue q;
return 0;
}
Explanation / Answer
//C++ Program to implementthe Queue ADT using an Array
#include<iostream.h>
#include <conio.h>
}
void insert()
{
if(rear==10)
cout<<"Queue is full"<<endl;
else
{
cout<<"Insert an element"<<endl;
cin>>ele;
q[rear]=ele;
rear++;
}
cout<<endl;
}
void delet()
{
if(front==rear)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Deleted elementis:"<<q[front]<<endl;
front++;
}
cout<<endl;
}
void display()
{
if(front==rear)
cout<<"Queue is empty"<<endl;
else
{
cout<<"The elements in the queueare:"<<endl;
for(int i=front;i<rear;i++)
cout<<q[i]<<" ";
}
cout<<endl;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.