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;
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;
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.
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.