My code is compiling out of order : <p>I'm using g++ on a Mac to compile a homework assignment. Any help is appreciated (also this isn't even the homework part of the homework, just the setup for the problem he asked us to complete). In my main.cpp I have </p>

<pre><code>Queue test_list;
test_list.add(4);
test_list.add(1);
test_list.add(3);
test_list.add(2);
test_list.remove();
cout &lt;&lt; "boop!" &lt;&lt; endl; 
</code></pre>

<p>I am compiling and linking together several files: g++ main.cpp queue.cpp -o Queue 
However my output is: </p>

<pre><code>4
End of test1
boop!
1
End of test1
3
End of test1
2
End of test1
</code></pre>

<p>I don't really understand why the code is compiling out of order- shouldn't the boop be at the end? I also don't understand how the code in my textbook (which is where I got the add and remove functions)works. I don't see how the remove function is repeating since it has no loop. 
Here they are for reference: </p>

<pre><code>void Queue::add(int item)
{
  if(empty())
  {
    front = new QueueNode;
    front-&gt;data = item;
    front-&gt;link = NULL;
    back = front;
  }
  else
  {
    QueueNodePtr temp_ptr;
    temp_ptr = new QueueNode;
    temp_ptr-&gt;data = item;
    temp_ptr-&gt;link = NULL;
    back-&gt;link = temp_ptr;
    back = temp_ptr;
  }
}
int Queue::remove()
{
  if (empty())
  {
    cout &lt;&lt; "Error: Removing an item from an empty queue.\n";
    exit(1);
  }
  int result = front-&gt;data;
  QueueNodePtr discard;
  cout &lt;&lt; result &lt;&lt; endl;
  discard = front;
  front = front-&gt;link;
  if (front == NULL)// if you are removing the last node
    back = NULL;
  delete discard;
  cout &lt;&lt; "End of test1" &lt;&lt; endl;
  return result;
}
</code></pre>
