trouble using classes

Discussion in 'OT Technology' started by notwist, Apr 14, 2005.

  1. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    alright. i started over and i keep getting this message:
    library.h: In member function `void Book::AddBook()':
    library.h:36: error: no match for 'operator>>' in 'std::cin >>
    temp->Book::bookID'

    here is my library.h
    Code:
         #include <iostream.h>
         
         using std::cout;
         using std::cin;
         using std::endl;
         
         class Book
         {
          public:
           char bookName[40];
           int bookID[10];
           int numCopies;
           Book *next;
         
         
         
           Book(int=0);
           void AddBook();
           
         
         };
         
           Book *current;
           Book *start_ptr = NULL;
         
         Book::Book(int copies) : numCopies(copies){}
         
         void Book::AddBook() //add book to end of list
         {
           Book *temp, *temp2;
           
           temp = new Book;
           cout << "\nEnter title of book: ";
           cin >> temp->bookName;
           cout << "\nEnter ID: ";
           cin >> temp->bookID;
           cout << "\nEnter number of copies: ";
           cin >> temp->numCopies;
           temp->next = NULL;
         
           if(start_ptr == NULL) //if list is empty
         	{
         	  start_ptr = temp;
         	  current = start_ptr;
         	}
           else
         	{
         	  temp2 = start_ptr;
         	  while(temp2->next != NULL)
         	temp2 = temp2->next;
         	  
         	  temp2->next = temp;
         	}
         }
           
         
         
    i'm also getting the error:
    library.cpp: In function `int main()':
    library.cpp:25: error: `AddBook' undeclared (first use this function)
    Code:
     #include <iostream.h>
     #include "library.h"
     
     int main(void)
     {
       int option;
     
       do
     	{
     	  option = 0;
     	  cout << endl;
     	  cout << "Select an option" << endl;
     	  cout << "1. Borrow Book" << endl;
     	  cout << "2. Return Book" << endl;
     	  cout << "3. Add New Book to Library" << endl;
     	  cout << "4. Remove Book from Library" << endl;
     	  cout << "5. Print Book Status" << endl;
     	  cout << "6. Print Library Status" << endl;
     	  cout << "7. Exit Library System" << endl;
     	  cout << endl << ">>";
       
     	  cin >> option;
     
     	  switch(option)
     	case 3 : AddBook();  break;
     
     	}
       while(option != 7);
     
     }
     
    its just supposed to add a new book object to the end of the link list. i have no idea what could be the problem. :wtc:
     
    Last edited: Apr 14, 2005
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    First problem is that Book::bookID is an array of ints. std::cin doesn't know what to do with an array of ints. It can handle single ints.

    Second problem, AddBook() doesn't work without an instance of class Book.
     
  3. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    alright i changed and switched some stuff around. actually i think this way is better but i'm now i'm getting this one single error. It says:
    library.h: In function `void AddBook()':
    library.h:28: error: request for member `bookName' in `temp', which is of
    non-class type `Book*'
    library.h:31: error: request for member `bookID' in `temp', which is of
    non-class type `Book*'
    library.h:34: error: request for member `numCopies' in `temp', which is of
    non-class type `Book*'


    Code:
      #include <iostream.h>
      
      class Book
      {
        public:
        char bookName[40];
        int bookID[10];
        int numCopies;
        Book *next;
      
        Book(int=0);
      
      };
      
      Book *current;
      Book *start_ptr = NULL;
      
      Book::Book(int copies) : numCopies(copies){}
      
      void AddBook() //add book to end of list
      {
        Book *temp, *temp2;
        
        temp = new Book;
        cout << "\nEnter title of book: ";
        cin >> temp.bookName;
      
        cout << "\nEnter ID: ";
        cin >> temp.bookID;
      
        cout << "\nEnter number of copies: ";
        cin >> temp.numCopies;
      
        temp->next = NULL;
      
        if(start_ptr == NULL) //if list is empty
      	{
      	  start_ptr = temp;
      	  current = start_ptr;
      	}
        else
      	{
      	  temp2 = start_ptr;
      	  while(temp2->next != NULL)
      	temp2 = temp2->next;
      	  
      	  temp2->next = temp;
      	}
      }
      
      void DisplayList()
      {
        Book *temp;
        temp = start_ptr;
        cout << endl;
        if (temp == NULL)
      	cout << "No books in library." << endl;
        else
      	{
      	  while (temp != NULL){
      	cout << "Title: " << temp->bookName << " ";
      	cout << "ID: " << temp->bookID << " ";
      	cout << "ID: " << temp->numCopies << endl;
      	temp=temp->next;
      	  }
      	  cout << "End of book list." << endl;
      	}
      }
             
    oh and btw, thanks for your help man. :hs:
     
    Last edited: Apr 14, 2005
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    You did nothing that addresses the problems I pointed out. The changes you made just make things worse.
     
  5. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    but it's only giving me the above error now :wtc:

    should cin.getline work?
    plus AddBook isn't a member of the Book class. so shouldn't it work?
     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The original problem was that bookID is declared as int bookID[10];
    The line that gave you the original error was:

    cin >> temp->bookID;

    What did you intend for this line to do?

    In your 2nd attempt, you went from temp->bookID to temp.bookID, yet temp is still a Book*. Using "." instead of the "->" is the cause of your current problem. Change it back to "->" and you still have to deal with the original problem.
     
  7. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    In your second post:
    you cannot dereference a pointer to an object using a period, use the proper -> dereferecing operation. Ex:

    cout << "\nEnter title of book: ";
    cin >> temp->bookName;
    cout << "\nEnter ID: ";
    cin >> temp->bookID;
    cout << "\nEnter number of copies: ";
    cin >> temp->numCopies;

    Even still, this won't work since cin doesn't know what to do with arrays. But skinjob mentioned this earlier and you didn't seem to understand that.
     
  8. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    if i change int bookID[10] to int bookID, will that change the old problem? i just want the addboko function to allow the user to enter data into the new Book class that is created.

    EDIT: alright i got it. Thanks for helping me out :hs:
     

Share This Page