使用堆栈和链接列表的C ++程序给了我额外的资格错误 [英] C++ program that uses a Stack and Linked List giving me an extra qualification error

查看:79
本文介绍了使用堆栈和链接列表的C ++程序给了我额外的资格错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据教程,我应该执行以下操作:

According to the tutorial, I am supposed to do the following:

Create a structure called AddressNode
    1. The structure should contain a string for name and a pointer called AddressNode that points to "next" (the next element in the list).

Create classed called ABook.
    1. Create the Default Constructure. (public)
    2. Create the Deconstructor. (public)
    3. AddressNode* topPtr. (private) – Should be a pointer to the top of the list. 

ABook Methods (each of these methods must be created in your program)
    1.	ABook::ABook() – Default Constructor
    2.	ABook::Insert(string NewItem) – Add item to the Linked List
    3.	ABook::SortedInsert(string NewItem) – Add item to the Linked List (sorted)
    4.	ABook::Remove(string& item) – Remove element from the top of the stack and return the item.
    5.	ABook::~ABook() – Deconstructor. Removes all elements from the list.

Main Method
    1.	Declare new List (Hint: ABook Book;)
    2.	Declare new string newName.
    3.	Declare new string nameToRemove.
    4.	Call Book.Insert("Precious"); - Insert Precious into list.
    5.	Call Book.SortedInsert("Ken"); - Insert Ken into listed (sorted)
    6.	Call Book.SortedInsert("Eileen"); - Insert Eileen into list (sorted)
    7.	Call Book.SortedInsert("Frank"); - Insert Frank into list (sorted)
    8.	Use Book.Remove to remove each name one at a time and display to screen.
    9.	Include: system("PAUSE"); after your output to pause the screen.





这是我到目前为止所做的。





And this is what I have done so far.

#include <iostream>
#include <string>
#include <cstdlib>

using std::cout;
using std::endl;
using std::string;
using namespace std;

//create and define the structure of the node
struct AddressNode                       //one element of list
{
   string name;                    //data item for names
   AddressNode* next;              //pointer to next element
};//end struct node

class ABook{//a list of links
   private:
      AddressNode* topPtr;                    //pointer to first link
   public:ABook()                    //default constructor
         { topPtr = NULL; }            //no first link
   ABook::~ABook();      //deconstructor. Removes all elements from the list.
   void ABook::Insert(string NewItem);   // Add item to the Linked List
   void ABook::SortedInsert(string NewItem);//Add item  (sorted)
   void ABook::Remove(string& item); /* Remove element from top of the stack and return the item.*/
   void ABook::print();//print All items
}; //end class ABook


//--------------------------------------------------------------//

 ABook::~ABook(){
 //deletes all the elements in the last as long as its not empty
  AddressNode* current = topPtr;
  while( topPtr != NULL )
   {
      current=topPtr->next;
      delete topPtr;
      topPtr=current;
   }
 } //end ABook deconstructor

//--------------------------------------------------------------//

void ABook::Insert(string NewItem){
 //add items to linked list
   AddressNode* newNode = new AddressNode();          //make a new node contain new item
   newNode->name = NewItem;                  //give it data
   newNode->next = topPtr;                   //it points to next link
   topPtr = newNode;                         //now first points to this

}//end ABook::Insert (function)

//--------------------------------------------------------------//

void ABook::SortedInsert(string NewItem){
   AddressNode* newNode = new AddressNode();          //make a new node contain new item
   newNode->name = NewItem;                  //give it data
   newNode->next=NULL;
   AddressNode* current = topPtr;       //set ptr to first link
   if(topPtr==NULL){
                    topPtr=newNode;
                    return;
   }
   if(NewItem.compare(topPtr->name) <= 0){
                                    newNode->next=topPtr;
                                    topPtr=newNode;
   }
   else{
        AddressNode *ptr = topPtr;
        if( ptr->next == NULL ){    // Reached end without inserting
                 ptr->next = newNode;       // Insert at end
                 newNode->next = NULL;
                 return;
        }
        do{

          if(NewItem.compare(ptr->next->name) <= 0){
                  newNode->next = ptr->next;  // Insert item after ptr
                  ptr->next = newNode;
                  break;
          }
          ptr = ptr->next ;
          if( ptr->next == NULL ){    // Reached end without inserting
                 ptr->next = newNode;       // Insert at end
                 newNode->next = NULL;
                 break;
          }
       }while(ptr->next != NULL);

   }
}//end ABook::SortedInsert(function)

//--------------------------------------------------------------//

void ABook::Remove(string& item){
   AddressNode* current = topPtr;       //set ptr to first link
   if(item.compare(topPtr->name)==0){
                                   topPtr=topPtr->next;
                                   return;
   }
   while( current->next != NULL )           //quit on last link
   {
      if(item.compare(current->next->name)!=0)current = current->next;           //move to next link
      else current->next=current->next->next;
   }
}
//--------------------------------------------------------------//
void ABook::print(){
   AddressNode* current = topPtr;       //set ptr to first link
   if(topPtr==NULL){
                    cout<<"List is null\n";
                    return;
   }
   cout<<"Sorted List: "<<endl;
   while( current != NULL )           //quit on last link
   {
      cout << current->name << endl;     //print data
      current = current->next;           //move to next link
   }
   cout<<endl;
}

int main(int argc, char** argv) {
   ABook Book;       //make linked list
   string nameToRemove1("Precious");
   string nameToRemove2("Eileen");
   string nameToRemove3("Frank");
   string nameToRemove4("Ken");
   Book.Insert("Precious");     // Insert Precious into list.
   Book.SortedInsert("Eileen"); // Insert Eileen into list (sorted)
   Book.SortedInsert("Frank");  // Insert Frank into list (sorted)
   Book.SortedInsert("Ken");    // Insert Ken into listed (sorted)
   Book.print();
   Book.Remove(nameToRemove1);
   Book.print();
   Book.Remove(nameToRemove2);
   Book.print();
   Book.Remove(nameToRemove3);
   Book.print();
   Book.Remove(nameToRemove4);
   Book.print();
   system("pause");
   return 0;
 }





我收到一系列错误,但我找不到任何关于它们的信息,甚至找不到它们。这些是错误





I am getting a series of errors, but I can't find any information about them or even identify them. These are the errors

22|| error: extra qualification 'ABook::' on member 'ABook'
23|| error: extra qualification 'ABook::' on member 'Insert'
24|| error: extra qualification 'ABook::' on member 'SortedInsert'
25|| error: extra qualification 'ABook::' on member 'Remove'
26|| error: extra qualification 'ABook::' on member 'print'





有人可以帮帮我吗?



Can anyone help me?

推荐答案

class ABook{//a list of links
   private:
      AddressNode* topPtr;                    //pointer to first link
   public:ABook()                    //default constructor
         { topPtr = NULL; }            //no first link
   ~ABook();      //deconstructor. Removes all elements from the list.
   void Insert(string NewItem);   // Add item to the Linked List
   void SortedInsert(string NewItem);//Add item  (sorted)
   void Remove(string& item); /* Remove element from top of the stack and return the item.*/
   void print();//print All items
}; //end class ABook





在类声明中,您不需要指定它所属的类型,因此您需要从方法名称中删除ABook ::。您只在执行方法时使用::,以便编译器知道它所属的位置。在类声明中,它假定属于声明类型。



In your class declaration, you don't need to specify the type that it belongs to, so you need to remove the ABook:: from the method names. You only use the :: when you are doing the implementation of the method so that the compiler knows where it belongs. Inside the class declaration, its assumed to belong in the declaring type.


这篇关于使用堆栈和链接列表的C ++程序给了我额外的资格错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆