error: no matching function for call to 'std::reference_wrapper<Medium>::reference_wrapper()
error: no matching function for call to 'std::reference_wrapper<Medium>::reference_wrapper()
Book
和 Article
从 Medium
.
导出 类
为什么我尝试在参考书目中插入 Medium
/ Book
/ Article
时出现此错误?
error: no matching function for call to '**std::reference_wrapper<Medium>::reference_wrapper()**
main.cc
#include <iostream>
using namespace std;
#include "Bibliography.h"
#include "Medium.h"
#include "Book.h"
#include "Article.h"
int main()
{
Bibliography p(1);
Medium m1("PN","I","Pasol nah",2017);
p.insert(m1);
cout << p;
return 0;
}
Bibliography.h
#ifndef BIBLIOGRAPHY_H_
#define BIBLIOGRAPHY_H_
#include "Medium.h"
#include "Article.h"
#include "Book.h"
#include <iostream>
#include <functional>
#include <vector>
class Bibliography
{
private:
int m_Size;
std::vector<std::reference_wrapper<Medium>> v;
int index;
public:
Bibliography(int size);
void insert(Medium m);
friend std::ostream& operator<<(std::ostream& out, const Bibliography &b1);
};
#endif
Bibliography.cc
#include "Bibliography.h"
Bibliography::Bibliography(int size)
{
std::cout << "Bibliography created \n";
m_Size = size;
v.resize(m_Size);
index = 0;
}
void Bibliography::insert(Medium m)
{
v.push_back(m);
}
std::ostream& operator<<(std::ostream& out, const Bibliography &b1)
{
for (Medium &Medium : b1.v)
{
out << Medium.toString() << std::endl;
}
return out;
}
你不应该在vector
中使用reference_wrapper
,因为vector
可以保留有默认构造函数的对象,reference_wrapper
没有,看看这些构造函数的 reference_wrapper
:
initialization (1)
reference_wrapper (type& ref) noexcept;
reference_wrapper (type&&) = delete;
copy (2)
reference_wrapper (const reference_wrapper& x) noexcept;
这一行
v.resize(m_Size);
您想创建 m_Size reference_wrapper
对象,但是 reference_wrapper
的默认构造函数不存在,代码无法编译。
您可以将 reference_wrapper
与 vector
一起使用,但会出现编译错误
调用vector方法时需要定义默认构造函数
Book
和 Article
从 Medium
.
为什么我尝试在参考书目中插入 Medium
/ Book
/ Article
时出现此错误?
error: no matching function for call to '**std::reference_wrapper<Medium>::reference_wrapper()**
main.cc
#include <iostream>
using namespace std;
#include "Bibliography.h"
#include "Medium.h"
#include "Book.h"
#include "Article.h"
int main()
{
Bibliography p(1);
Medium m1("PN","I","Pasol nah",2017);
p.insert(m1);
cout << p;
return 0;
}
Bibliography.h
#ifndef BIBLIOGRAPHY_H_
#define BIBLIOGRAPHY_H_
#include "Medium.h"
#include "Article.h"
#include "Book.h"
#include <iostream>
#include <functional>
#include <vector>
class Bibliography
{
private:
int m_Size;
std::vector<std::reference_wrapper<Medium>> v;
int index;
public:
Bibliography(int size);
void insert(Medium m);
friend std::ostream& operator<<(std::ostream& out, const Bibliography &b1);
};
#endif
Bibliography.cc
#include "Bibliography.h"
Bibliography::Bibliography(int size)
{
std::cout << "Bibliography created \n";
m_Size = size;
v.resize(m_Size);
index = 0;
}
void Bibliography::insert(Medium m)
{
v.push_back(m);
}
std::ostream& operator<<(std::ostream& out, const Bibliography &b1)
{
for (Medium &Medium : b1.v)
{
out << Medium.toString() << std::endl;
}
return out;
}
你不应该在vector
中使用reference_wrapper
,因为vector
可以保留有默认构造函数的对象,reference_wrapper
没有,看看这些构造函数的 reference_wrapper
:
initialization (1)
reference_wrapper (type& ref) noexcept;
reference_wrapper (type&&) = delete;
copy (2)
reference_wrapper (const reference_wrapper& x) noexcept;
这一行
v.resize(m_Size);
您想创建 m_Size reference_wrapper
对象,但是 reference_wrapper
的默认构造函数不存在,代码无法编译。
您可以将 reference_wrapper
与 vector
一起使用,但会出现编译错误
调用vector方法时需要定义默认构造函数