1

I am making a school project and I need to create a library system with bidirectional list and structures. I want to implement constructors to make my code more clear. The problem shows up when i want to creacte struct for bidirectional list and reserve memory space for list element using new list_of_books. The error msg I am getting is no matching function call to Book::Book() and the default constructor of "list_of_books" cannot be referenced -- it is deleted function. What does it mean and how can I fix that?

struct User {
  std::string name;
  std::string surname;
  User(std::string name, std::string surname) 
    : name(name), surname(surname) {}
};

struct Date {
  int day;
  int month;
  int year;
  Date(int day, int month, int year) 
    : day(day), month(month), year(year) {}
};

struct Book {
  int id;
  std::string title;
  struct User author;
  std::string cathegory;
  struct Date hire_date;
  struct User reader;
  std::string others;
  Book(int id, std::string title, std::string nameA, std::string surnameA, std::string cathegory, int day, int month, int year, std::string nameR, std::string surnameR, std::string others) 
    : id(id), title(title), author(nameA, surnameA), cathegory(cathegory), hire_date(day, month, year), reader(nameR, surnameR), others(others) {}
};

struct list_of_books {
  struct Book book;
  list_of_books* next;
  list_of_books* prev;
};

void push(Book data) {
  if (head == NULL) {
    list_of_books* element = new list_of_books;
    element->book = data;
    element->prev = NULL;
    element->next = NULL;
    head = element;
  } else {
    list_of_books* curr = head;

    while(curr->next != NULL) {
      curr = curr->next;
    }

    list_of_books* element = new list_of_books;
    element->book = data;
    element->prev = curr;
    element->next = NULL;
    curr->next = element;
  }
}
BooBoo
  • 89
  • 1
  • 7
  • TL;DR: you only get a default constructor for free if you don't define a constructor yourself. Does this answer your question? [Is there an implicit default constructor in C++?](https://stackoverflow.com/questions/563221/is-there-an-implicit-default-constructor-in-c) – scohe001 Dec 23 '20 at 14:07
  • I don't think so, the sollution I want to achieve is to have constructor in Book struct, but don't use it in list_of_books struct. I think the error creates because the list struct inherits constructor from Book struct but I am not using it. So I need to somehow use Book struct in list_of_books struct without using book constructor. – BooBoo Dec 23 '20 at 14:12
  • you are using it in `struct list_of_books` it has a member `struct Book book;` and you attempt to create it via `list_of_books* element = new list_of_books;`. How do you want to create the member `book` when you create the `list_of_books` ? – 463035818_is_not_an_ai Dec 23 '20 at 14:15

1 Answers1

3
struct User {
  std::string name;
  std::string surname;
  User(std::string name, std::string surname) 
    : name(name), surname(surname) {}
};

this constructor is harmful. You have an aggregate -- a type that is its data -- and a constructor that just repeats the members in order.

Do this:

struct User {
  std::string name;
  std::string surname;
};

and repeat for every other constructor you wrote.

Constructors that do nothing but repeat the arguments in order are not good things.

If you remove every constructor in your program, your program will compile.


Now what is going wrong? By creating a constructor, you delete the default no-argument constructor.

Then when you new list_of_books, it tries to use the constructor for Book, which doesn't exist.


Note that when working with aggregates, you have to use {} brace construction lists if you want to construct them in place, like Book b = {"The Bookiest Book", "Bob Smith"};, instead of () for the arguments.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Oh, okey, I didn't know that i shouldn't creating that type of constructors and I didn't even know about that type of declaration ```Book b = {...}```. It helps a lot. I created constructors, because I need to read data from .txt file and I can't use ```getline()``` because I am saving it to diffrenet types of struct variables and I thought that constructors will help me. Thanks a lot for the answer. – BooBoo Dec 23 '20 at 14:46
  • 1
    @booboo also do `int day=0;` etc; those ctors can be useful, but if you add them the default ctor is deleted. – Yakk - Adam Nevraumont Dec 23 '20 at 15:48