0

I am new in C++ programming. I have exprience in python but c++ class is a bit confusing for me. I have search for this problem but can't seem to figure out the problem. Can any one help me?

#include <iostream>
using namespace std;

class Book
{
public:
    string title, author;
    Book(string t, string a)
    {
        title = t;
        author = a;
    }
    void display()
    {
        cout << "Title: " << title << endl;
        cout << "Author: " << author << endl;
    }
};

class Card
{
public:
    Book books[100];
    int numberOfBooks = 0;

    void store(Book book)
    {
        if (numberOfBooks < 100)
        {
            books[numberOfBooks] = book;
            numberOfBooks++;
        }
        else
        {
            cout << "No more space" << endl;
        }
    }

    void display()
    {
        for (int i = 0; i < numberOfBooks; i++)
        {
            books[i].display();
        }
        cout << "Number of books: " << numberOfBooks << endl;
    }
};



int main()
{
    Book b1 = Book("The Lord of the Rings", "J.R.R. Tolkien");
    Book b2 = Book("Harry Potter", "J.K. Rowling");
    Book b3 = Book("The Hunger Games", "Suzanne Collins");

    Card c1 = Card(); // the default constructor of "Card" cannot be referenced -- it is a deleted function
    c1.store(b1);
    c1.store(b2);
    c1.store(b3);
    c1.display();
    return 0;
    
}

So the problem is when i declare the class it's showing 'the default constructor of "Card" cannot be referenced -- it is a deleted function' this.

sifat
  • 353
  • 1
  • 8
  • `Cart` has no compiler-generated default constructor because `Book` has no default constructor. – paolo Jul 26 '22 at 07:18
  • 3
    It's because of `Book books[100];` In order to initialize a `Book` you need to specify the title and author, and you need to initialize 100 books. I recommend using `std::vector` or similar here. With `std::vector` you can add `Book`s as you need them instead of having to make all of them all at once. – user4581301 Jul 26 '22 at 07:18
  • Can you show me an example? – sifat Jul 26 '22 at 07:19
  • A good simple example of [`std::vector` use at the bottom of this documentation page](https://en.cppreference.com/w/cpp/container/vector). – user4581301 Jul 26 '22 at 07:21
  • Also note that C++ and Python handle objects very differently. C++'s default behaviour is to pass by value rather than by reference, so `c1.store(b1);` copies `b1` into `store`'s `book` parameter. That copying can get quite expensive, so prefer to pass large or complicated (and `Book` is made large and complicated by the `std::string`s it contains) objects by reference (`void store(Book &book)`) and in many cases by `const` reference to prevent accidents. – user4581301 Jul 26 '22 at 07:26
  • 1
    In the linked example you can also see, how to simplify the `for` loop with `vector` – Sebastian Jul 26 '22 at 07:28
  • 3
    @Sifat Refer to a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and this [dupe](https://stackoverflow.com/questions/71910536/error-no-matching-function-for-call-to-pointpoint). – Jason Jul 26 '22 at 07:35

0 Answers0