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

  • Home
  • SEARCH
  • 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 8184319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:27:49+00:00 2026-06-07T01:27:49+00:00

i have a class book and 2 subclass lerningbook,readingbook and i am trying to

  • 0

i have a class book
and 2 subclass
lerningbook,readingbook
and i am trying to create a list of books
book* listofbooks
and add to it the subclass

Book* listOfBooks;

void Mang::addBookToList(Book b3)
{
    Book* temp;
    temp=listOfBooks;
    lobsize++;
    listOfBooks=new Book[lobsize];
    int i;
    for(;i<lobsize;i++)
    {
        listOfBooks[i]=temp[i];
    }
    listOfBooks[i]=b3;
}

problem is when i am trying to send it the subclass this not accpecting it
i tryed to use template so the function will be able to take any class but it didnt help

the error am geting is:
no suitable user defined conversion from lerningbook to book exsists
guessing i need to implement same type of function that will allow me to do this but i dont know wich one
hope one of you can help me out thx in adv 🙂

(i know i am missing the delete[] on the temp array sort of got stuck on this problem)

  • 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-06-07T01:27:51+00:00Added an answer on June 7, 2026 at 1:27 am

    b3 is a Book, but listoOfBooks is an array of Book*. You need to pass a Book* to addBookToList, not just for the assigment but to avoid object slicing.

    If this is not a learning exercise, use a std::vector<Book*> instead or a std::vector<std::shared_ptr<Book>>. The std::vector will dynamically grow as required and the use of a smart pointer will automatically delete the elements when the vector is destroyed.

    If you choose use of the Book* ensure you obey the What is The Rule of Three?. This could just be making Mang non-copyable by declaring the copy constructor and assignment operator private.

    Note that i is uninitialised in for loop and the following assignment is out of bounds access on the array as i == lobsize after the for:

    listOfBooks[i]=b3;
    

    array indexes run from 0, so lobsize - 1 is the index of the last element.

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

Sidebar

Related Questions

Class User has_many_belongs_to_many :books end Class Book has_many_belongs_to_many :users end I'm trying to create
I have a class that has an list<Book> in it, and those Book objects
I have a class that has an list<Book> in it, and those Book objects
I have the class book: @Entity @Table(name = books) @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) @XmlType public class
I have a class Book defined and I want to create a JAXBElement object
Suppose, I have models: public class Person { public sting Name {get;set;} public List<Book>
I have a class Book which has some annotations like @Override , @Before ,
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have a book class, then a novel- and a science book class that
I have one grails application.In that I have one model class named Book. From

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.