Reputation: 23
Sorry that this is a rather large question. I cannot get the following C++ to work, I always get an error about not having a struct/class/union in the code from visiual studio. I am supposed to have the books getting put into a linked list in alphabetical order, but so far my insert method is broken.
//********************************************************************
// BookList.cpp
//
// Represents a collection of books.
//*******************************************************************
#include "BookList.h"
//----------------------------------------------------------------
// Creates a new Book object and adds it to the end of
// the linked list.
//----------------------------------------------------------------
void BookList::add(Book *newBook) {
BookNode *node = new BookNode(newBook);
BookNode *current;
if (head == NULL)
head = node;
else {
current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = node;
}
}
char *BookList::getBookList(char *list) {
list[0] = '\0';
BookNode *current = head;
while (current != NULL) {
strcat( list, current->book->getBook() );
strcat( list, "\n" );
current = current->next;
}
return list;
}
void BookList::insert(Book *newBook) {
BookNode *node = new BookNode(newBook);
BookNode *current;
if (head == NULL) {
head = node;
}
else {
current = head;
int result = *newBook.compareTo(current->book->getBook());
if (result == -1) {
current->next = node;
}
else {
while (result == 1) {
current = current->next;
result = *newBook.compareTo(current->book->getBook());
}
current->next = node;
}
}
}
//********************************************************************
// BookList.h
//
// Represents a collection of books.
//*******************************************************************
#include "Book.h"
class BookNode {
public:
BookNode() { };
BookNode(Book *theBook) {
book = theBook;
next = NULL;
};
friend class BookList;
private:
Book *book;
BookNode *next;
};
class BookList {
public:
void add(Book *);
char* getBookList(char *);
void delet(Book *);
void insert(Book *);
BookList() {
head = NULL;
};
private:
BookNode *head;
};
#include <cstring>
//********************************************************************
// Book.h
//
// Represents a single book.
//*******************************************************************
class Book {
public:
Book (char *newTitle) {
strcpy( title, newTitle );
}
int compareTo(Book *newBook) {
int compvar;
compvar = strcmp(newBook->getBook(), title);
return compvar;
}
char *getBook() {
return title;
}
private:
char title[81];
};
There are certainly multiple problems with this code, so any help anybody can provide would be awesome. Thanks in advance!
Upvotes: 0
Views: 1601
Reputation: 2803
This line of code is wrong (two lines that look just like this):
int result = *newBook.compareTo(current->book->getBook());
If you dereference, use parenthesis around it:
int result = (*newBook).compareTo(current->book->getBook());
But to make it easier to read, I would suggest:
int result = newBook->compareTo(current->book->getBook());
This should show you your real problem: compareTo() is expecting a Book object and getBook() returns a char *. Use the following and it compiles fine for me. Without a main function that uses the classes, I couldn't tell you if your code logically works right but now it compiles.
You should really overload the equality operator in Book.h for comparisons. Hope this helps.
Upvotes: 1
Reputation: 1343
You need to add a forward declaration of BookList
class before you start declaring the BookNode
class as you have the line friend class BookList;
inside the definition of your BookNode
class.
Change the following:
//********************************************************************
// BookList.h
//
// Represents a collection of books.
//*******************************************************************
#include "Book.h"
class BookNode {
to this:
//********************************************************************
// BookList.h
//
// Represents a collection of books.
//*******************************************************************
#include "Book.h"
class BookList;
class BookNode {
Without this, the compiler will complain that BookList
is not a struct/class/union.
Obviously you'll have to get rid of the other compiler errors in your code after doing this.
Upvotes: 0
Reputation: 181
Well, the logical error that jumps out at me is that you never say
node->next = current->next
before
current->next = node
Of course, there are STL libraries for linked lists, there's no bounds-checking in your printing function (which should probably be using a std::string anyway), and this never cleans up the memory it allocates... but I assume this is for an intro programming assignment. You won't be able to instantiate your class without a delet() method defined either.
Upvotes: 1