Valhalla Legends Forums Archive | .NET Platform | C++/CLI - Strange behaviour in a FIFO queue?

AuthorMessageTime
Dyndrilliac
Edit: I feel rather silly now. After lots of thinking, I realized that I had forgotten to append the carot to my class name when retrieving a newly initialized instance. I also realized that my constructor call was ill-formed. The code has been fixed and re-pasted if anyone wants to use the queue.

I'm not sure quite how to describe the bug I'm facing - perhaps I should show you my output.[quote]Value: SimpleQueue
Value: String
Value: 34.6
Press any key to continue . . .[/quote]
The first value being displayed seems to have come from absolutely nowhere. SimpleQueue (as you'll see below) is the class name for an object in my program. However, what I don't know is how it found it's way to the output screen. There isn't any reference to the string in my code, and nothing in particular jumps out as the cause (to me). I would appreciate anyone with suggestions on how to best solve the issue.

Here's the code (this is all of it, I have no idea what might be causing the anomaly).
[code]    using namespace System;

    public ref class SimpleQueue {

        private:

            ref struct Node {

                Node^  p;
                Node^  n;
                Object^ o;

                Node() {
                    this->n = nullptr;
                    this->p = nullptr;
                    this->o = nullptr;
                }

                Node(Node^ nn) {
                    this->n = nn->n;
                    this->p = nn->p;
                    this->o = nn->o;
                }

                Node(Node^ np, Node^ nn, Object^ oo) {
                    this->n = nn;
                    this->p = np;
                    this->o = oo;
                }

            };

            Int16 size;
            Node^ head;
            Node^ tail;

        public:

            SimpleQueue() {
                this->Clear();
            }

            void Clear() {
                this->head = gcnew Node(nullptr, tail, nullptr);
                this->tail = gcnew Node(head, nullptr, nullptr);
                this->size = 0;
            }

            void Push(Object^ o) {
                if (this->size >= (Int16::MaxValue - 1)) {
                    throw gcnew Exception("Queue is full.");
                }

                if (this->Empty) {
                    Node^ t = gcnew Node(head, tail, o);
                    this->head->n = t;
                    this->tail->p = t;
                    this->size = 1;
                } else {
                    Node^ g = gcnew Node(head, head->n, o);
                    Node^ k = head->n;
                    k->p = g;
                    this->head->n = g;
                    this->size++;
                }
            }

            Object^ Pop() {
                if (this->Empty) {
                    throw gcnew Exception("Queue is empty.");
                }

                Object^ o = tail->p->o;

                Node^ temp = gcnew Node(tail->p);
                temp = temp->p;
                temp->n = tail;
                tail->p = temp;

                return (o);
            }

            property Int16 Size {
                Int16 get() {
                    return (this->size);
                }
            }

            property bool Empty {
                bool get() {
                    return (this->size == 0);
                }
            }
    };

    int main() {

        SimpleQueue^ sq = gcnew SimpleQueue;

        sq->Push("String");
        sq->Push(34.6);

        long x = sq->Size;

        for (long i = 0; i < x; i++) {
            Console::WriteLine("Value: {0}", sq->Pop());
        }

        return 0;
    }[/code]
November 16, 2006, 9:22 AM
Myndfyr
If you need a FIFO queue, why not just use System.Collections.Generic.Queue<T>?
November 17, 2006, 2:22 AM
Dyndrilliac
I never said I was going to use the queue. Besides, I need to know and understand the methodology behind creating a queue of my own.
November 17, 2006, 9:30 PM

Search