Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a queue of queue of custom defined objects (using a custom defined queue template)
    text
    copied!<p>I am using the following code to construct a queue of 10 queues, each containing 10 <code>person</code> objects consisting of three member variabless. Every member variable is different from the others. Later on, I cycle through the queues using a pair of nested <code>for</code> loops to print out all the attributes, however when I do so, rather than 100 uniquely different values, I am simply receiving a set of garbage data. I have tried many things to diagnose the problem, but to no avail. Could someone please check out the code below and see if they can spot where I'm going wrong?</p> <p>Note: the original code is more complex than this, but I have identified the adding of the queues to the 'master queue' as being the source of the problem, so I have simplified the code somewhat to make it easier to focus on the parts that matter. If anyone require any further information, the please let me know.</p> <pre><code>string UserChoiceStr; int UserChoiceInt; bool Valid = false; queue&lt;book&gt; BookList; queue&lt;string&gt; WaitingListIndex; queue&lt;queue&lt;person&gt; &gt; WaitingLists; int main() { Initialise(); MainMenu(); return 0; } void Initialise() { queue&lt;person&gt; PersonQueue; for (int count1 = 1; count1 &lt;= 10; count1 ++) { for (int count2 = 1; count2 &lt;= 10; count2 ++) { person PersonObject(1, "String goes here", 2); PersonQueue.Add(PersonObject); } WaitingLists.Add(PersonQueue); } } </code></pre> <p>queue.h:</p> <pre><code>#ifndef QUEUE_H #define QUEUE_H using namespace std; template &lt;class Type&gt; class queue { private: Type *Contents; int Front, Back; int QueueSize; public: queue(int queuesize = 10); ~queue(); bool Empty() const; bool Full() const; bool Remove(Type&amp;); bool Add(Type&amp;); }; #endif </code></pre> <p>queuetemplate.h</p> <pre><code>#ifndef QUEUETEMPLATE_H #define QUEUETEMPLATE_H #include "queue.h" using namespace std; // Constructor template &lt;class Type&gt; queue&lt;Type&gt; :: queue(int queuesize): Front(0), Back(0), QueueSize(queuesize), Contents(new Type[queuesize + 1]) {} // Destructor template &lt;class Type&gt; queue&lt;Type&gt; :: ~queue() { delete[] Contents; } // Tests template &lt;class Type&gt; bool queue&lt;Type&gt; :: Empty() const { return (Front == Back) ? true : false; } template &lt;class Type&gt; bool queue&lt;Type&gt; :: Full() const { return ((1 + Back) % (QueueSize + 1) == Front) ? true : false; } // Push and pop template &lt;class Type&gt; bool queue&lt;Type&gt; :: Remove(Type&amp; FrontElement) { if (Empty()) { return false; } else { FrontElement = Contents[Front]; Front = (Front + 1) % (QueueSize + 1); return true; } } template &lt;class Type&gt; bool queue&lt;Type&gt; :: Add(Type&amp; NewElement) { if(Full()) { return false; } else { Contents[Back] = NewElement; Back = (Back + 1) % (QueueSize + 1); return true; } } #endif </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload