不能引用默认构造函数

我正在做一个学校项目,需要创建一个具有双向列表和结构的图书馆系统。我想实现构造函数以使代码更清晰。当我想为双向列表创建结构并使用来为列表元素保留存储空间时,就会出现问题new list_of_books我收到的错误消息是没有对Book :: Book()的匹配函数调用,并且无法引用默认的“ list_of_books”构造函数-该函数已删除这是什么意思,我该如何解决?

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;
  }
}
Yakk-亚当·内夫罗蒙特
struct User {
  std::string name;
  std::string surname;
  User(std::string name, std::string surname) 
    : name(name), surname(surname) {}
};

这个构造函数是有害的。您有一个聚合(即为其数据的类型)和一个仅按顺序重复成员的构造函数。

做这个:

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

并为您编写的所有其他构造函数重复上述步骤。

只按顺序重复这些参数的构造函数不是一件好事。

如果删除程序中的所有构造函数,程序将进行编译。


现在出了什么问题?通过创建构造函数,可以删除默认的无参数构造函数。

然后,当您new list_of_books尝试使用的构造函数时Book,它不存在。


请注意,在使用聚合时,{}如果要在适当的位置构建它们,则必须使用大括号构建列表,例如Book b = {"The Bookiest Book", "Bob Smith"};,而不是()参数。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

不能引用测试夹具的默认构造函数

来自分类Dev

在构造函数中,为什么在执行超类构造函数之前不能引用当前类的字段?

来自分类Dev

为什么我不能引用我的匿名函数?

来自分类Dev

移动构造函数不能默认

来自分类Dev

类型引用不能引用容器

来自分类Dev

#selector的参数不能引用属性

来自分类Dev

委托方法不能引用自己的类

来自分类Dev

使用声明不能引用类成员

来自分类Dev

为什么不能引用刷新按钮?

来自分类Dev

为什么我不能引用指向实例化对象函数的指针?

来自分类Dev

构造函数不能采用非const引用

来自分类Dev

默认移动构造函数和引用成员

来自分类Dev

尽管非默认构造函数参数,也会在const引用成员上调用默认构造函数

来自分类Dev

隐式“ this”不能引用Xamarin中的扩展方法

来自分类Dev

为什么我不能引用attr`android:popupEnterTransition`?

来自分类Dev

角度ui模态不能引用父范围

来自分类Dev

在声明之前不能引用局部变量“ request”

来自分类Dev

为什么我不能引用图像数组?

来自分类Dev

不能引用赛普拉斯全局变量

来自分类Dev

静态成员不能引用类类型参数。

来自分类Dev

委托方法不能引用其自己的类

来自分类Dev

通用从站的实际表达式不能引用信号

来自分类Dev

在声明之前不能引用局部变量“ request”

来自分类Dev

我不能引用一个类 - 某处的问题

来自分类Dev

为什么在隐藏变量时不能引用变量?

来自分类Dev

Visual Studio 2015中的“无法引用默认构造函数”

来自分类Dev

无法引用默认构造函数错误(Visual Studios 2019)

来自分类Dev

c ++对默认构造函数的未定义引用

来自分类Dev

constexpr默认的默认构造函数

Related 相关文章

热门标签

归档