我收到一条错误消息“未在范围内解析”

时间:2016-10-11 01:15:20

标签: c++ scope linked-list

这是一个链接列表程序分配,我相信我差不多完成了该程序。在main方法中,我一直在这些行上得到4个错误。我想知道是否有人能告诉我范围有什么问题。感谢名单

  

错误:未在此范围内声明“addFirst”        addfirst仅(书目);

     

错误:未在此范围内声明“addLast”        addlast仅(书目);

     

错误:未在此范围内声明'isInTheList'        isInTheList(书目);

     

错误:未在此范围内声明'deleteBook'        deleteBook(书目);

#include <iostream>
    #include <string>
    #include "Program3.h"
    #include <limits>       // for numeric limits

    using namespace std;

//Implementation for class BookList START

void BookList::addFirst(BookNode * book){ //To add a new head node
        book -> setNext(head);
        head = book;

    }

void BookList::addLast(BookNode * book){ //To add a new node at the end

        if(!head){
            addFirst(book);
        }
        else{

            BookNode * temp = head;
            while(temp->getNext() != NULL){

                    temp = temp->getNext();
                }
            temp -> setNext(book);
        }
    }   

void BookList::traverse(){  //TO go through the whole linked list

        BookNode * temp = head;
        while(temp != NULL){
            std::cout << temp -> getTitle()<<std::endl;
            temp = temp -> getNext();
        }
    }


bool BookList::isInTheList(std::string title){  //To check the linked list for a specific title

        BookNode * temp = head;
        while(temp != NULL){
            if(temp -> getTitle() == title){
                return true;
            }
            temp = temp -> getNext();
        }

        return false;
    }



bool BookList::deleteBook(std::string title){   //to delete a book/title


        if(head == NULL){ // or if(!head)  To check if the linked list is empty
            return false;
        }

        BookNode * prev = NULL;
        BookNode * cur = head;


        if( cur -> getTitle() == title){    //Checking if the title is the first node
            head = head -> getNext();
            delete cur;
            return true;
        }

        while(cur != NULL && cur -> getTitle() != title){   //Checking through the linked list for the title

            prev = cur;                                     //The while loop will stop when it has gone though the whole linked list or if it matches with the title
            cur = cur -> getNext();
        }

        if(cur == NULL){        //There is no matching entry

            return false;
        }
        else{

            prev -> setNext( cur -> getNext() );
            delete cur;
            return true;
        }
    }

BookList::~BookList(){



        while( head != NULL){

            string tempTitle = head->getTitle();
            deleteBook(tempTitle);
            cout << "Book " << tempTitle << "has been deleted" <<endl;



        }





    }   


int getUserChoice(){

    int choice = 0;

    cout << "Welcome to the e-library, please make a choice from the menu below" << endl;
    cout << "1. Add a book at the beginning" << endl;
    cout << "2. Add a book at the end" << endl;
    cout << "3. Find a book in the list" << endl;
    cout << "4. Delete a book in the list" << endl;
    cout << "5. Print all the books in the list" << endl;
    cout << "6. Exit " << endl;

    if(cin >> choice){  //confirming that cin succeeded

        if(choice > 0 && choice < 7){

            cin.ignore(); //dump newline character
            return choice;
        }
        else{
            cin.ignore();//dump newline character
            return 0;
        }
    }
    else{

        cin.clear(); //bring cin back from failed status
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        return 0;
    }
}









int main(){


    BookList booklist;  //Create an object of ArrayList

    int choice = getUserChoice();   //Prompting user for an input

    while (choice != 6 ){   

        switch(choice){

            case 0: 
                cout << "Invalid choice. Please Choose between 1 and 6." <<endl; // 0 is not a given choice, loop reruns
                break;
            case 1: 
                addFirst(booklist);     
                break;
            case 2:
                addLast(booklist);
                break;
            case 3:
                isInTheList(booklist);
                break;
            case 4:
                deleteBook(booklist);
                break;
            case 5:
                booklist.traverse();
                break;
            default:
                break;
        }

        choice = getUserChoice(); // prompt user to enter the choice again



    return 0;

    }       

}   

然后我的头文件在同一文件夹中保存为Program.h,代码如下

#ifndef PGM_03_H//<-
#define PGM_03_H//<- both these need to have the same name

#include <string>


using namespace std;

/*class definition for BookNode
 * which is non-compatible. i.e it has a pointer to the next BookNode
 */


class BookNode{

    private : 
        std::string bookTitle;
        BookNode * next;
    public : 
        //default constructor
        BookNode(){bookTitle = "";next = NULL;} 

        //custom constructor
        //it initializes book with title        
        BookNode(string title){
            bookTitle = title;
            next = NULL;

        }

        //getter functions
        std::string getTitle(){
            return bookTitle;

        }

        BookNode * getNext(){
            return next;

        }

        //setter functions
        void setTitle(std::string newTitle){
            bookTitle = newTitle;

        }

        void setNext(BookNode * newNext){
            next = newNext;

        }

};


/*
* class definition for BookList
* which is a linked list that uses object of BookNode as node.
* it has only one variable: head, which is a BookNode pointer.
*/

class BookList{

    private:
        BookNode * head;



    public:
        //default constructor
        BookList(){
            head = NULL;

        }


        //destructor, which will be called automatically
        //it deletes all nodes in the linkedlist
        ~BookList();

        //add new node as the first node in the booklist
        void addFirst(BookNode *);


        //add new node as the last node in the booklist
        void addLast(BookNode *);


        //traverse function. It will print out info on BookNode
        void traverse();


        //check if the given book is in the list
        bool isInTheList(std::string);

        //delete the given book
        //return true if it was deleted
        //return false if it was not found
        bool deleteBook(std::string);



};





#endif

1 个答案:

答案 0 :(得分:1)

addFirst()等是BookList类的成员,但是您将它们称为函数。您需要一个BookList类的实例来处理这些方法。

您还要将BookList传递给期待addFirst的{​​{1}}。你希望它看起来像:

BookNode