-1

Hi I am implementing a basic linked-list based dynamic Queue class and when running the test I get "bus error : 10". I am new with using terminal (MAC OS X) and I would like some help finding what is wrong with my code or maybe the way I am trying to run it. It compiles good.

Links to code:

HEADER FILE

CPP FILE

TEST CODE:

//
//  Test Program 1 for Dynamic Int Queue Class
//
#include <iostream>   // for I/O facilities
#include "queue.h" //  basic_int_queue declarations
using namespace std;

int main (int argc, char * const argv[]) {

   bool OK = true ;
   Queue q1 ;
   if( ! q1.empty() ) OK = false ;
   q1.enqueue(1);
   if( q1.empty() ) OK = false ;
   q1.enqueue(2);
   q1.enqueue(3);
   q1.enqueue(4);
   if( q1.front() != 1 )  OK = false ;
   q1.dequeue();
   q1.dequeue();
   q1.dequeue();
   if( q1.front() != 4 )  OK = false ;
   if( q1.dequeue() != 4 ) OK = false ;
   if( ! q1.empty() ) OK = false ;

   if( OK ){
      cout << "GOOD" << endl ;
      return 0 ;
   }else{
      cout << "BAD" << endl ;
      return -1 ;
   }
}

THANK YOU!

Alicia Pavan
  • 27
  • 1
  • 5
  • You might consider building your program with the compiler option `-g` to generate debug information and then run your application through a debugger like `gdb`. It should tell you where your bus error is. – Michael Petch Oct 30 '14 at 03:16

1 Answers1

0

In your queue.cpp file enqueue implementation, you need to allocate a new node on the heap using new. For example, changing the following lines in your enqueue

node * temp;
temp->next = NULL;
temp->data = item;

to

node * temp = new node(item, NULL);

will get rid of the bus error.

Also, at the end of your dequeue implementation, you need to

delete temp;

before

return item;

Otherwise the delete temp; line is unreachable, and you'd have memory leaks.

One last thing, normally you should

#include "queue.h"

in your queuen.cpp. Not the other way around.

hushaohan
  • 83
  • 5
  • Your help was useful but now I am getting this: >>Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) What does it mean?? – Alicia Pavan Oct 30 '14 at 19:06
  • Looks like you were trying to compile an executable using just the `queue.cpp`. You need to also have the test cpp file (the file containing the code you pasted in your original post) in your compile command. So something like `g++ queue.cpp test.cpp`. – hushaohan Oct 30 '14 at 19:22