C++:STD::EpthiTrink后台没有用字符串值类编译

C++:STD::EpthiTrink后台没有用字符串值类编译,c++,perfect-forwarding,C++,Perfect Forwarding,结果: /* Item.h */ struct Item { std::string name; Item(std::string _name) : name( std::move(_name) ) { } }; /* main.cpp */ /* ... */ const int amount_of_items = val.size(); std::vector<Item> items(amount_of_items); for( Json::Value::c

结果:

/* Item.h */
struct Item {
    std::string name;

    Item(std::string _name) : name( std::move(_name) ) { }
};

/* main.cpp */
/* ... */
const int amount_of_items = val.size();
std::vector<Item> items(amount_of_items);

for( Json::Value::const_iterator itr = val.begin() ; itr != val.end() ; ++itr ) {
    items.emplace_back( "item_name" );
}

我不知道为什么这不起作用-有什么想法吗?

您的结构中没有默认构造函数,因为您添加了一个非默认构造函数。您可以重新添加默认构造函数

但错误行实际上是您的项目初始化量,这是尝试使用默认的c'tor来构建项目的默认值

因此,我建议您添加一个默认的c'tor:

/usr/include/c++/8/bits/stl_construct.h:75:7: error: no matching function for call to ‘Item::Item()’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from main.cpp:8: Item.h:8:2: note: candidate: ‘Item::Item(std::__cxx11::string)’   Item(std::string _name) : name( std::move(_name) ) { }   ^~~~ Item.h:8:2: note:   candidate expects 1 argument, 0 provided
或使用字符串初始化:

    Item() = default;

您的结构中没有默认构造函数,因为您添加了非默认构造函数。您可以重新添加默认构造函数

但错误行实际上是您的项目初始化量,这是尝试使用默认的c'tor来构建项目的默认值

因此,我建议您添加一个默认的c'tor:

/usr/include/c++/8/bits/stl_construct.h:75:7: error: no matching function for call to ‘Item::Item()’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from main.cpp:8: Item.h:8:2: note: candidate: ‘Item::Item(std::__cxx11::string)’   Item(std::string _name) : name( std::move(_name) ) { }   ^~~~ Item.h:8:2: note:   candidate expects 1 argument, 0 provided
或使用字符串初始化:

    Item() = default;
调用std::vector构造函数时:

    Item() = default;

    //             here
    //               V
    explicit Item(std::string &&_name) : name( std::move(_name) ) { }
std::vector<Item> items(amount_of_items);
或者使用其他没有此要求的std::vector构造函数之一:

Item() = default
调用std::vector构造函数时:

    Item() = default;

    //             here
    //               V
    explicit Item(std::string &&_name) : name( std::move(_name) ) { }
std::vector<Item> items(amount_of_items);
或者使用其他没有此要求的std::vector构造函数之一:

Item() = default
本声明:

std::vector<Item> items(amount_of_items, {""});
因为emplace_back会根据需要增加阵列

请注意,如果您想提前为项目预留空间,可以调用items.reserve。

此语句:

std::vector<Item> items(amount_of_items, {""});
因为emplace_back会根据需要增加阵列


请注意,如果您想预先为项目预留空间,可以调用items.reserve。

问题是您需要此代码序列的默认Item构造函数。默认构造函数已禁用,因为您有项的自定义构造函数

您可以通过添加Item=default;来启用它

除此之外,还有一个逻辑错误:std::vector itemsamount\u of_items;启动项,使其达到默认可构造元素的项的数量。根据下一个序列,这不是您想要的,因为到最后,您将拥有两倍的元素数

你应该写信的

std::vector<Item> items;

问题是您需要此代码序列的默认Item构造函数。默认构造函数已禁用,因为您有项的自定义构造函数

您可以通过添加Item=default;来启用它

除此之外,还有一个逻辑错误:std::vector itemsamount\u of_items;启动项,使其达到默认可构造元素的项的数量。根据下一个序列,这不是您想要的,因为到最后,您将拥有两倍的元素数

你应该写信的

std::vector<Item> items;