我收到一个错误,说"Not decaled in scope"
I get an error saying "Not decaled in scope"
这是一个链表程序的作业,我相信我几乎完成了这个程序。在主方法的这几行中,我一直得到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
addFirst()
等是BookList
类的成员,但是您调用它们就好像它们只是函数一样。您需要一个BookList类的实例来处理这些方法。
您还将BookList
传递给期待BookNode
的addFirst
。你希望它看起来像:
booklist.addfirst(node);
相关文章:
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- OpenCV Android C++ imwrite not found
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 错误"Could not find Boost"(缺少:上下文标头)
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- Is !NaN not a NaN?
- 为什么我会" void value not ignored as it ought to be"?
- 解决"ld: library not found for -ltensorflow_framework.2.3.0"
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- Directx 11 - CompileFromFile() is not compiling
- 方法错误"not all control paths return a value"和方法不返回值
- Centos7 g++ "to_string is not in a member of std"
- 将系数存储在头文件的数组中("does not name a type"错误)
- QGraphicsItems not showing up QGraphicsScene
- 在调试模式下引发C++ "deque iterator not dereferencable"异常
- InitializeCriticalSectionEx Not Located In KERNEL32.Dll
- 我收到一个错误,说"Not decaled in scope"