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!
std::in front of several functions such asstd::strcpy. 3) Don't usestrcpy. 4) Don't say81.