Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C++
#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?
Posted

1 solution

C++
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


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.
 
Share this answer
 
v2
Comments
lotussilva 1-Jul-13 15:54pm    
You're a genius my friend! Thank you! and thank you for the explanation and helping me understand the problem.
lotussilva 1-Jul-13 16:16pm    
I have a question on this....

if(item.compare(topPtr->name)== 0){
topPtr = topPtr->next;
return;
}


The tutorial came with chunks of code. And this is one part I couldn't read very well. What is the logic behind this lines?
H.Brydon 1-Jul-13 16:47pm    
If the node to remove is at the head of the list, then move the head of the list to the next node. This removes the first item from the list. It also creates a memory leak. In a non-GC language such as C++, the things you are no longer interested in should be deleted, not just dropped on the floor. In fact all code paths in your Remove() method will leak. Bad juju.

Look at the specs for the Remove() function. It is not supposed to do that.

You should also flag your question with 'Homework' tag.
lotussilva 1-Jul-13 17:31pm    
Thank you, that explains a lot. It's actually not Homework. I graduated years ago as a Software Eng :) But I focused on Web Development and now I'm trying to relearn C++/JAVA. This was in a book I was given.

Thanks again!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900