How to call a function method in a class from another class

I need to call compareTo method , which is in "class Book" .
The compareTo is called in another class named "BookList".
the concept is to pass pointer "current" as only one argument by having a pointer "node" as a calling argument.
I've structed in this step for days.


Thank you for your time.



in main function
1
2
3
4
5
int main(int argc, char* argv[]) {

BookList *books = new BookList();

books->insert (new Book("F Title") ) // insert function//; 



in class BookList
1
2
3
4
5
6
7
8
9
10
11
12
class BookList {
      public:
      void insert(Book *);  // insert function //
      void delet (Book *);
      char* getBookList(char *);

      BookList() {
         head = NULL;
      };

   private:
      BookNode *head;




in class Book
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Book {

   public:
      Book (char *newTitle) {
         strcpy( title, newTitle );
      }
	  Book () {};

      char *getBook() {
         return title;
      }
	
           //
           //add Compare method --> my problem //
           //
           //
	   int compareTo(Book nextBook) {
		   return strcmp(getBook() , nextBook.getBook());   }
	 

   private:

	   

      char title[81];




some part of definition of insert function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void BookList::insert(Book *newBook) 
{

   BookNode *node = new BookNode(newBook);
   BookNode *current;
   BookNode *previous;  
   

   
   if (head == NULL)
       head = node;

   
   else 
   {
		current = head;
		node->next = head;	 
		head = node;
	  
	  // 
          //
          //
          // Here is my question . I couldn't call the function and compare them
          // because the compiler says I pass pointer or something
          // it couldn't convert Book* to Book or some thing like this.
          //
          //
          //
	  while (node->book->compareTo(current->book) > 0 ) && (current != NULL))
	  {
		  previous = current;
		  current = current->next;
		 



Last edited on
1
2
3
4
	  while (node->book->compareTo(current->book) > 0 ) && (current != NULL))
//                     ^                        ^
//                     │                        └─ here, book would then also be a pointer to a Book
//  here, book appears to be a pointer to a Book 

The compareTo function takes a Book as a parameter (not a pointer to a Book).

Try:
while (node->book->compareTo(*current->book) > 0 ) && (current != NULL))

Also note that current must not be NULL prior to using it, you are checking for that after using it.
Last edited on
Try changing

compareTo(current->book)

to

compareTo(*(current->book))

Also, for efficiency

int compareTo(Book nextBook)

may be better as

int compareTo(const Book & nextBook)

-Rollie
Is this supposed to be an exercise or are you not aware of std::string and std::list?
dear moorecm
I don't think this works coz current->book is already a pointer. If I misunderstand the concept , please kindly give me more explanation then I can write these thing on my own later.
I don't want only solve the problem but also deeply understand it. I appreciate your help.
1
2
//try
while (node->book->compareTo(*current->book) > 0 ) && (current != NULL))




dear rollie
That's a good idea. I'll try it when I'm home. Anyway , how can I refer to title of node
and title of current. if this will work?

Call compareTo (which is in Book) in BookList
1
2
3
4
5
6
7
8
9
10
11
12

//
//
// would it ok to call compareTo function by using node->book->compareTo
//I tried node->book.compareTo
//both of them failed
//
//I'm not sure it's because of the way I call the compareTo (node->book->compareTo)  or 
//the way I pass the parameter
//
//
node->book->compareTo(*(current->book))






compareTo's definition
1
2
3
4
int compareTo(const  Book & nextBook){
		   return strcmp(title , nextBook.title;   }

@arther
this exercise is about link list insertion , but my textbook does not show how to call the function if we have pointer->pointer->class
and I include all of std.

Topic archived. No new replies allowed.