0

---------------------Queue.h--------------------

#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
#include "MyException.h"

using namespace std;

template<class T>
class Queue;

template<class T>
ostream& operator<<(ostream&,Queue<T>&);

template<class T>
class Queue
{
    public:
        friend ostream& operator<< <T>(ostream&,Queue<T>&);
        Queue();
        Queue(const Queue<T>& other);
        Queue<T>& operator=(const Queue<T>& other);
        ~Queue();
    void enqueue(const T& el);
        T dequeue();
        void increasePriority(const T& el);
        bool isEmpty();

    private:
        class Node
        {
            public:
                Node(const T& data, Node* n = 0)
                {
                    element = data;
                    next = n;
                }

                T element;
                Node* next;
        };
        Node* head;

};

#include "Queue.C"

#endif

.h (上記) ファイルを変更することはまったく許可されていません。

---------------------Queue.C--------------------

 #include "Queue.h"

    template<class T>
    ostream& operator << (ostream &strm, Queue<T> &obj)
    {
        if (obj.isEmpty())
        {
            strm << "[]";
            return strm;
        }
        else
        {
            strm << '[';
 //line 28
    Node* tmp = new Node();
            tmp = obj.head;
            strm << tmp.element + ',';
            while (tmp->next != 0)
            {
                tmp = tmp->next;
                if (tmp-next != 0)
                {
                    strm << tmp.element + ',';
                }
                else
                {
                    strm << tmp.element;
                }
            }
            strm << ']';
            delete [] tmp;
            tmp = 0;
            return strm;
        }
        return strm;
    }
    //...more code
    Queue::Queue()
    {
//line 54
    head = new Node();
        }

そのコードから、私が受け取るエラーのいくつかは次のとおりです。

Queue.C: In function ‘std::ostream& operator<<(std::ostream&, Queue<T>&)’:
Queue.C:28: error: ‘Node’ was not declared in this scope
Queue.C:28: error: ‘tmp’ was not declared in this scope
Queue.C:28: error: expected type-specifier before ‘Node’
Queue.C:28: error: expected ‘;’ before ‘Node’
Queue.C:34: error: ‘next’ was not declared in this scope
Queue.C: At global scope:

Queue.C:54: error: ‘head’ was not declared in this scope
Queue.C:54: error: expected type-specifier before ‘Node’
Queue.C:54: error: expected ‘;’ before ‘Node’
4

2 に答える 2

1

Node定義されたスコープをプレフィックスとして付ける必要があります。Queue

Queue<T>::Node* tmp = new Queue<T>::Node();

そうしないと、コンパイラはあなたがどのNodeタイプを意味するのかわかりません。

tmpしかし、次の行では、ポインタをで上書きしobj.head、新しく作成したを失いますNode。これにより、メモリリークが発生します。いずれの場合もNode、キューを出力するだけの演算子で新しいを作成する必要はありません。

さらに下に、あなた

delete [] tmp;

これにより、キューの最後の要素が削除されます。この出力演算子では、キューを変更しないでください。

次のポイントにつながる、この演算子を宣言する必要があります

friend ostream& operator<< <T>(ostream&, const Queue<T>&);

キューオブジェクトを誤って変更しないようにするため。

于 2013-02-25T06:57:49.570 に答える
1

Queue.hQueue.C、またはその逆を含めています。ヘッダーから実装全体にアクセスできるようにするため、 に含めないQueue.hでください。Queue.C

次に、Nodeは内部で宣言されているため、実装では、クラス テンプレートQueueのスコープをプレフィックスとして付ける必要があります。Queue

Queue<T>::Node ...; // instead of Node
于 2013-02-25T06:52:33.460 に答える