Dies ist für eine verknüpfte Liste Programmzuweisung und ich glaube, ich bin fast fertig mit dem Programm. Ich bekomme 4 Fehler in diesen Zeilen in der Hauptmethode. Ich fragte mich, ob jemand mir sagen könnte, was mit dem Umfang falsch ist. ThanxIch bekomme eine Fehlermeldung "Nicht deklassiert im Bereich"
error: ‘addFirst’ was not declared in this scope addFirst(booklist);
error: ‘addLast’ was not declared in this scope addLast(booklist);
error: ‘isInTheList’ was not declared in this scope isInTheList(booklist);
error: ‘deleteBook’ was not declared in this scope deleteBook(booklist);
#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;
}
}
und dann wird meine Header-Datei als Program.h im selben Ordner und der Code ist gespeichert, wie unten
#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
Es sollte nicht dekaliert deklariert werden ... – Hasi
Sie können Ihre Frage bearbeiten, um Tippfehler zu beheben. Das wäre auch eine gute Gelegenheit, all diesen Code durch ein [mcve] zu ersetzen. –