Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7524799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:09:35+00:00 2026-05-30T03:09:35+00:00

Sorry that this is a rather large question. I cannot get the following C++

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:09:36+00:00Added an answer on May 30, 2026 at 3:09 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question about using Visual Studio 2003. Sorry it is not strictly
This is going to sound too silly / too basic - sorry about that,
Sorry if this is a stupid question... I've developed an application that creates absolute
First of all, sorry about that title. I'm not the best at writing those
Sorry for the rather subjective question, but I was hoping to get an opinion
Before answering this question, understand that I am not asking how to create my
(Sorry this is rather a vague question. My attempts to be clearer [and indeed
Sorry for the odd question but this is one that makes me want to
somewhat related to: libxml2 from java yes, this question is rather long-winded - sorry.
Sorry if this is a dup; I haven't found any questions that pose quite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.