0

私はリストとイテレータクラスの関数を書いていますが、ほぼ完了していますが、メインファイルにいくつかのエラーが発生します。リストのremove_all関数を書くと、それを削除してもどこにもエラーはありません。理由を知っている!!また、実際には、イテレータ演算子とbool Iterator::is_item()

どんな助けでも大歓迎です。ありがとう

ここに私のコードがあります:

Node.h

pragma once

namespace list_1
{
    template <typename T>
    struct Node
    {
        T data;
        Node<T> *next;


        // Constructor
        // Postcondition: 
        Node<T> (T d);
    };

    template <typename T>
Node<T>::Node(T d)
{

}
}

Iterator.h

// Template CLASS PROVIDED: Iterator 

#pragma once
#include "Node.h"

namespace list_1
{
    template<typename T>
    class Iterator
    {
    public:
        Iterator<T> (Node<T> *np);
        // precondition: is_item is true
        // post condition n points to the next item in the list
        void operator++();
        // precondition: 
        // postcondition: returns true if there is a valid item
        bool is_item();
        // precondition: is_item == true
        // postcondition returns data that n is pointing at
        T operator* ();

    private:
        Node<T>* n;

    };

List.h

#ifndef LIST_H
#define LIST_H
#include "Node.h"
#include "Iterator.h"

namespace list_1
{
    template <typename T>
    class list
    {
    public:
        // CONSTRUCTOR
        list( );
        // postcondition: all nodes in the list are destroyed.
        ~list();
        // MODIFICATION MEMBER FUNCTIONS
        //postcondition: entry is added to the front of the list
        void insert_front(const T& entry);
        //postcondition: entry is added to the back of the list
        void add_back(const T& entry);
        // postcondition: all nodes with data == entry are removed from the list
        void remove_all(const T& entry);
        // postcondition: an iterator is created pointing to the head of the list
        Iterator<T> begin(void);

        // CONSTANT MEMBER FUNCTIONS
        // postcondition: the size of the list is returned
        int size( ) const;
    private:
        Node<T>* head;

    };
4

1 に答える 1

1

構文エラーは、remove_all 関数の最後にある「else」ブロックのいくつかの閉じ中括弧が欠落しているためです。

これに置き換えてみてください

(編集:上記のコメントに記載されているcoutに関する提案を含めました)

void list<T>::remove_all(const T& entry)
{
    if(head == 0)
    {
        std::cout<<" node cannot be delted";

    }
    else
    {
        Node<T> *curr = head;
        Node<T> *trail = 0;

        while( curr != 0)
        {
            if(curr->entry == entry)
            {
                break;
            }
            else
            {
                trail = curr;
                curr = curr->next;
            }
        }
            if(curr == 0)
            {
                std::cout<<" Node " << entry<< " is not found";
            }
            else
            {
                if ( head == curr)
                {
                    head = head->next;
                }
                else
                {
                    trail->next = curr->next;
                }
            } // missing this one
     delete curr;
     } // and this one as well
}
于 2013-05-17T02:17:50.913 に答える