Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 奇数std::vector::emplace()编译错误_C++_C++11_Vector_Compiler Errors_Emplace - Fatal编程技术网

C++ 奇数std::vector::emplace()编译错误

C++ 奇数std::vector::emplace()编译错误,c++,c++11,vector,compiler-errors,emplace,C++,C++11,Vector,Compiler Errors,Emplace,我在使用std::vector::emplace()和std::vector::emplace\u back()时遇到一个奇怪的编译器错误: #包括 结构Foo{ int-bar; Foo(intbar):bar({} }; int main(){ //宣言1 std::vec(10); //宣言2 //std::向量向量向量{}; 向量后置(1); 返回0; } 当我执行此操作时,会出现以下错误: In file included from /usr/include/c++/6/vector

我在使用
std::vector::emplace()
std::vector::emplace\u back()时遇到一个奇怪的编译器错误

#包括
结构Foo{
int-bar;
Foo(intbar):bar({}
};
int main(){
//宣言1
std::vec(10);
//宣言2
//std::向量向量向量{};
向量后置(1);
返回0;
}
当我执行此操作时,会出现以下错误:

In file included from /usr/include/c++/6/vector:62:0,
                 from prog.cpp:2:
/usr/include/c++/6/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = Foo; _Args = {}]’:
/usr/include/c++/6/bits/stl_uninitialized.h:519:18:   required from ‘static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int; bool _TrivialValueType = false]’
/usr/include/c++/6/bits/stl_uninitialized.h:575:20:   required from ‘_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int]’
/usr/include/c++/6/bits/stl_uninitialized.h:637:44:   required from ‘_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = Foo*; _Size = long unsigned int; _Tp = Foo]’
/usr/include/c++/6/bits/stl_vector.h:1309:36:   required from ‘void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/6/bits/stl_vector.h:281:30:   required from ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Foo>]’
prog.cpp:11:25:   required from here
/usr/include/c++/6/bits/stl_construct.h:75:7: error: no matching function for call to ‘Foo::Foo()’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:7:2: note: candidate: Foo::Foo(int)
  Foo(int _bar) : bar(_bar) { }
  ^~~
prog.cpp:7:2: note:   candidate expects 1 argument, 0 provided
prog.cpp:4:8: note: candidate: constexpr Foo::Foo(const Foo&)
 struct Foo {
        ^~~
prog.cpp:4:8: note:   candidate expects 1 argument, 0 provided
prog.cpp:4:8: note: candidate: constexpr Foo::Foo(Foo&&)
prog.cpp:4:8: note:   candidate expects 1 argument, 0 provided
在/usr/include/c++/6/vector:62:0中包含的文件中,
来自项目cpp:2:
/usr/include/c++/6/bits/stl_-construct.h:在“void std::_-construct(_-T1*,_-Args&&…[带_-T1=Foo;_-Args={}]”的实例化中:
/usr/include/c++/6/bits/stl_uninitialized.h:519:18:来自“静态”前向迭代器std::“未初始化”后的必填项默认值1::“未初始化”后的默认值(_ForwardIterator,_Size)[其中_ForwardIterator=Foo*;_Size=long unsigned int;bool _tinalivaluetype=false]”
/usr/include/c++/6/bits/stl_uninitialized.h:575:20:从“_forwardIteratorstd::__uninitialized_default_n(_ForwardIterator,_Size)[带_ForwardIterator=Foo*;_Size=long unsigned int]”中需要
/usr/include/c++/6/bits/stl_uninitialized.h:637:44:从“_forwarditeratorstd::_uninitialized_default_n_a(_ForwardIterator,_Size,std::allocator&)[带_ForwardIterator=Foo*;_Size=long unsigned int;_Tp=Foo]”中必需
/usr/include/c++/6/bits/stl_vector.h:1309:36:从“void std::vector::_M_default_initialize(std::vector::size_type)[with _Tp=Foo;_Alloc=std::Alloc=分配器;std::vector::size_type=long unsigned int]中需要
/usr/include/c++/6/bits/stl_vector.h:281:30:std::vector::vector(std::vector::size_type,const allocator_type&)中的必填项[with the(with)Tp=Foo;_Alloc=std::Alloc=allocator;std::vector::size_type=long unsigned int;std::vector::allocator_type=std::allocator]
进度cpp:11:25:从这里开始需要
/usr/include/c++/6/bits/stl_construct.h:75:7:错误:调用'Foo::Foo()'时没有匹配的函数
{::新建(静态_-cast(u-p))_-T1(标准::转发(u-args));}
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
程序cpp:7:2:注:候选人:Foo::Foo(int)
Foo(intbar):bar({}
^~~
prog.cpp:7:2:注意:候选者需要1个参数,提供0个参数
程序cpp:4:8:注:候选:constexpr Foo::Foo(const Foo&)
结构Foo{
^~~
prog.cpp:4:8:注意:候选者需要1个参数,提供0个参数
程序cpp:4:8:注:候选人:constexpr Foo::Foo(Foo&&)
prog.cpp:4:8:注意:候选者需要1个参数,提供0个参数

但是,如果我将声明1注释掉,而使用声明2,代码可以编译。这是怎么回事?

您的问题不在于
vec.emplace\u back(1);
。您的编译错误是因为
std::vec(10);
。该行试图创建一个包含10个默认构造元素的向量。由于您的类没有默认构造函数,因此无法创建10个默认元素

为了让它工作,你需要提供一个类的实例,它可以复制到向量中

std::vector<Foo> vec(10, Foo(whatever_number_you_want));
std::vector vec(10,Foo(任何你想要的数字));
或者您可以只添加一个默认构造函数



std::vector vec{};
不会给您带来任何问题,因为它不会尝试默认构造任何元素。空构造函数返回大小为0的向量,这意味着没有构造任何对象,从而避免了未定义的默认构造函数。

原因是
std::vector vec(10)
将实例化一个包含10个“空”Foo对象的向量,即需要调用默认构造函数的类
Foo
的实例。但是,您的类
Foo
不提供默认构造函数

第二条语句
std::vector vec{}
实例化了一个空向量,因此没有实例化
Foo
-对象(这需要一个默认构造函数)

要解决您的问题,请在
Foo
中定义默认构造函数:

struct Foo {
    int bar;

    Foo() : bar(0) {};
    Foo(int _bar) : bar(_bar) { };
};
struct Foo {
    int bar;

    Foo() : bar(0) {};
    Foo(int _bar) : bar(_bar) { };
};