的初始化没有匹配的构造函数
error: no matching constructor for initialization of
我知道这个问题以前有人问过,但请哼唱我。我很难理解如何初始化一个类。
这是有问题的代码。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//------------------------------------------------------------------- ---------------------------
class Date //Class Date
{
public:
int day;
int month;
int year;
Date();
Date(int,int,int);
~Date(void);
};
Date::Date(void)
{
day = 0;
month = 0;
year = 0;
}
Date::Date(int month, int day, int year)
{
day = day;
month = month;
year = year;
} //Class Date
//---------------------------------------------------------------------------------------------
//Class Book
class Book
{
public:
string _title;
string _author;
Date _published;
string _publisher;
float _price;
string _isbn;
int _page;
int _copies;
Book();
Book(string,string,Date,string,float,string,int,int);
~Book(void);
};
Book::Book(void)
{
_title = "";
_author = "";
//_published;
_publisher = "";
_price = 0;
_isbn = "";
_page = 0;
_copies = 0;
}
Book::Book(string title, string author, Date published, string publisher, float price, string isbn, int page, int copies)
{
_title = title;
_author = author;
_published = published;
_publisher = publisher;
_price = price;
_isbn = isbn;
_page = page;
_copies = copies;
} //Class Book
//---------------------------------------------------------------------------------------------
class Node //Class Node
{
friend class LinkedList;
private:
Book *_book;
Node *_next;
public:
Node(void);
Node(Book*);
Node(Book*,Node*);
~Node(void);
};
Node::Node(void)
{
_book = NULL;
_next = NULL;
}
Node::Node(Book *book)
{
_book = book;
_next = NULL;
}
Node::Node(Book *book, Node *next)
{
_book = book;
_next = next;
} //Class Node
//---------------------------------------------------------------------------------------------
class LinkedList //Class LinkedList
{
private:
Node *_head;
Node *_tail;
public:
LinkedList(void);
LinkedList(Book*);
~LinkedList(void);
void insert_front(Book*);
void insert_rear(Book*);
void print_list(void);
};
LinkedList::LinkedList(void)
{
_head = NULL;
_tail = NULL;
}
LinkedList::LinkedList(Book *book)
{
_head = new Node(book);
_tail = _head;
} //Class LinkedList
//---------------------------------------------------------------------------------------------
void LinkedList::insert_front(Book *book)
{
if(_head == NULL)
{
_head = new Node(book);
_tail = _head;
}
else
_head = new Node(book, _head);
}
void LinkedList::insert_rear(Book *book)
{
if(_head == NULL)
{
_head = new Node(book);
_tail = _head;
}
else
{
_tail -> _next = new Node(book);
_tail = _tail -> _next;
}
}
void LinkedList::print_list(void)
{
Node *temp = _head;
while(temp!= NULL)
{
cout << temp -> _book -> _title << endl;
cout << temp -> _book -> _author << endl;
cout << temp -> _book -> _publisher << endl;
temp = temp -> _next;
cout << endl;
}
}
LinkedList::~LinkedList(void)
{
}
//---------------------------------------------------------------------------------------------
//Main
int main(void)
{
LinkedList myList;
ifstream myFile("input.txt");
string title;
string author;
Date published; // was "Date published(int,int,int);"
string publisher;
float price;
string isbn;
int page;
int copies;
while(myFile)
{
getline(myFile,title);
getline(myFile,author);
//getline(myFile,published);
getline(myFile,publisher);
//getline(myFile,price);
getline(myFile,isbn);
//getline(myFile,page);
//getline(myFile,copies);
myList.insert_front(new Book(title,author,published,publisher,price,isbn,page,copies));
}
myList.print_list();
return 0;
}
我感兴趣的错误是:
assignment3.cpp:213:33:错误:没有匹配的初始化构造函数"书"……书(标题、作者、出版、出版人、价格、isbn、页面,复制));^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Assignment3.cpp:67:7:注意:候选构造函数不可行:未知第三个参数从'Date (int, int, int)'转换为'Date'Book::Book(字符串标题,字符串作者,出版日期,字符串出版…)^Assignment3.cpp:38:7:注:候选构造函数(隐式复制构造函数)不可行:需要1个参数,但提供了8个参数类的书^Assignment3.cpp:54:7:注意:候选构造函数不可行:需要0参数,但提供了8个书:本书(空白)^生成1个错误。
我做了建议的改变,现在我得到一个不同的错误:
架构x86_64的未定义符号:"Date::~Date()",引用自:Book::Book() in assignment3-0f3b1c.oBook::Book(std::__1::basic_string, std::__1::allocator>, std::__1::basic_string, std::__1::allocator>, Date, std::__1::basic_string, std::__1::allocator>, float, std::__1::basic_string, std::__1::allocator>, int, int) in assignment3-0f3b1c.o_main in assignment3-0f3b1c没有找到架构x86_64的Ld:符号Clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)
下面是一个函数声明(published
,取3个int
,返回Date
):
Date published(int,int,int)
你想创建一个变量:
Date published;
或者,如果你想明确表示你关心零初始化:
Date published{};
相关文章:
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- 在C++中使用默认构造函数初始化对象的不同方法
- 使用默认构造函数初始化对象的不同方法
- 我们可以用参数化构造函数初始化结构的数组吗?
- 有没有办法使用该类的构造函数初始化另一个类的私有部分内的对象数组?
- 类内初始化与构造函数初始化列表的顺序
- 通过 C++ 中的重载构造函数初始化未知类型的变量
- 使用复制构造函数初始化 new[]
- 如何从子类的构造函数初始化父类的私有成员
- 在构造函数初始化列表中使用 std::variant
- 使用构造函数初始化结构还是在之后设置其值更好?
- C++ 没有匹配的构造函数初始化 []
- 我正在使用dev c ++,但收到错误(C++98'array'必须由构造函数初始化)
- 使用所述填充构造函数初始化向量中的向量
- 如何同时创建一个具有两个或多个构造函数初始化的对象
- 在 new 关键字中,由默认构造函数初始化的类中的元素是否也使用 new 关键字在C++?
- 这是使用构造函数初始化数组对象的最佳方法吗?
- 内置类型的构造函数初始化
- 构造函数初始化和对象损坏