Fatal Error: One Unresolved External
Apr 30, 2014 at 5:01am UTC
I'm trying to implement this queue class, and I don't know why it won't compile. I wrote a main function in the main.cpp file and even commented out the calls to functions add and removed and still gave me the fatal error.
queue.h
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
#ifndef QUEUE_H
#define QUEUE_H
namespace queuesavitch
{
struct QueueNode
{
char data;
char next;
QueueNode *link;
};
typedef QueueNode* QueueNodePtr;
class Queue
{
public :
Queue();
Queue(const Queue& aQueue);
~Queue();
void add(char item);
char remove();
bool empty() const ;
void displayqueue();
private :
QueueNodePtr front;
//QueueNodePtr next;
QueueNodePtr back;
int count;
};
}
#endif
main.cpp
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
#include <iostream>
#include "queue.h"
using namespace queuesavitch;
using namespace std;
Queue::Queue()
{
front = NULL;
back = NULL;
}
bool Queue::empty() const
{
return count == 0 ? true : false ;
}
void Queue::add(char item)
{
// Create a new node
QueueNode* tmp = new QueueNode;
tmp->data = item;
tmp->link = NULL;
if ( empty() )
{
// Add the first item
front = back;
back = tmp;
}
else
{
// Append to the list
back->link = back;
back = tmp;
}
}
char Queue::remove()
{
if ( empty() )
{
cout << "Queue is empty" ;
}
char ret = front->data;
QueueNode* tmp = front;
// Move the front pointer to next node
front = front->link;
count--;
delete tmp;
return ret;
}
void Queue::displayqueue()
{
cout << "You entered: \n" ;
while (! empty())
cout << remove();
cout << endl;
}
int main()
{
Queue q;
char next, ans;
do
{
cout << "Enter a name:\n" ;
cin.get(next);
while (next != '\n' )
{
cin.get(next);
q.add(next);
}
//q.add(item);
q.remove();
q.displayqueue();
cout << "Again?(y/n): " ;
cin >> ans;
cin.ignore(10000, '\n' );
}while (ans != 'n' && ans != 'N' );
system ("pause" );
return 0;
}
Apr 30, 2014 at 5:09am UTC
You have not implemented the destructor.
May 2, 2014 at 2:31am UTC
Okay, I've implemented the instructor, but now I have an "Access violation at writing location 0x00000004."
1 2 3 4 5
{
// Append to the list
back->link = back;
back = tmp;
}
May 2, 2014 at 8:51am UTC
0x00000004 is very close to 0 (null) so my guess is that you are dereferencing a null pointer somewhere.
May 2, 2014 at 8:59am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
void Queue::add(char item)
{
// Create a new node
QueueNode* tmp = new QueueNode;
tmp->data = item;
tmp->link = NULL;
if ( empty() )
{
// Add the first item
front = back;
back = tmp;
}
else
{
// Append to the list
back->link = back;
back = tmp;
}
}
When you create a queue, it is empty and
front and
back are set to null. Here, when you encounter an empty list and add a character, you set
front equal to
back or null, then set
back to point to
tmp .
Any attempt to use
front past that point will result in dereferencing a null pointer.
Topic archived. No new replies allowed.