我正在尝试使用C ++进行项目,并且试图输入从整数类型向量到成对类型向量的值。
我尝试了emplace_back
和push_back
,但是它不起作用。很有可能是由于成对输入的向量。
注意:我正在使用Boost库,不确定是否有帮助。
typedef unsigned int objecttype;
typedef string operationtype;
typedef unsigned int checkob;
typedef pair<objecttype, operationtype> objectops;
vector<vector<unsigned int>> parameters;
//lines returning values to “parameters”
vector<checkob> checkParams = parameters.at(0); // works fine
vector<objectops> objectParams = parameters.at(1); // below error
错误消息:
error test.cpp:273:30: error: no viable conversion from 'vector<unsigned int, allocator<unsigned int>>' to 'vector<model:: objectops, allocator<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >>>'
stl_vector.h:326:7: note: candidate constructor not viable: no known conversion from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<unsigned int, std::allocator<unsigned int> > > >::value_type' (aka 'std::vector<unsigned int, std::allocator<unsigned int> >') to 'const std::vector<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > &' for 1st argument
stl_vector.h:344:7: note: candidate constructor not viable: no known conversion from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<unsigned int, std::allocator<unsigned int> > > >::value_type' (aka 'std::vector<unsigned int, std::allocator<unsigned int> >') to 'std::vector<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > &&' for 1st argument
stl_vector.h:383:7: note: candidate constructor not viable: no known conversion from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<unsigned int, std::allocator<unsigned int> > > >::value_type' (aka 'std::vector<unsigned int, std::allocator<unsigned int> >') to 'initializer_list<std::vector<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::value_type>' (aka 'initializer_list<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >') for 1st argument
答案 0 :(得分:1)
您正在尝试将std::vector<unsigned int>
分配给std::pair<unsigned int, std::string>
,这显然是不可能的。
您可以
std::pair<unsigned int, std::string>
的向量分配给
objectParams
unsigned int
的无符号int egers(即parameters
)向量的向量
和一个std::string
。您可能是这个意思:
vector<objectops> objectParams ={ { parameters.at(0).at(0), "string"} };
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ std::pair<objecttype, operationtype>
// ^^ ^^ for std::vector<objectops>