我目前正在做一个大学作业,现在我正在努力与向量。
我应该返回一个对象的唯一ID,然后将该对象添加到向量中。
该对象是一个结构体,定义如下:
struct VertexPuller{
std::vector<InVertex> head_settings;
std::vector<IndexType> indexing;
};
我想推到的向量是
std::vector<std::unique_ptr<VertexPuller>> vertex_puller_tables;
我写的函数看起来像这样:
auto vertex_puller= std::make_unique<VertexPuller>;
auto vp_id = reinterpret_cast<VertexPullerID>(vertex_puller);
vertex_puller_tables.push_back(std::move(vertex_puller));
return vp_id;
然而,在倒数第二行,当我试图将顶点拉入向量时,我得到错误-No matching member function for call to 'push_back'。
我已经在这个问题上停留了很长一段时间,我不知道是什么原因导致了这个问题,可能是指针,就像我和C一样。谢谢你的建议!
3条答案
按热度按时间d7v8vwbk1#
方法
push_back
在那里。您发送的类型可能不匹配。尝试读取编译错误并找出预期的类型和发送的实际类型。一个更简单的例子,同样的错误:
编译错误是:
然而,如果我们进一步阅读,它说:
gojuced72#
vertex_puller
是一个std::make_unique<VertexPuller>
函数。它不是一个unique_ptr<VertexPuller>
。你必须调用这个函数,并传递任何你要传递给VertexPuller
构造函数的参数。46scxncf3#
[No调用“push_back”的匹配函数ans.push_back(output)][1]
第7行:字符13:错误:没有匹配的成员函数用于调用'push_back' ans.push_back(output);/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1184:7:注:候选函数不可行:对于第一个参数push_back(常量值类型&__x)^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../include/c++/9/bits/stl_vector.h:1200:7:没有已知的从“整数”到“常量标准::向量标准::__cxx11::基本字符串〈字符,标准::分配器标准::_cxx11::基本字符串〉::值类型”的转换注:候选函数不可行:没有已知的转换从'int'到'std::vectorstd::__cxx11::basic_string〈char,std::allocatorstd::__cxx11::basic_string〉::value_type'(aka 'std::__cxx11::basic_string')for 1st argument push_back(value_type& __x)^ [1]:https://i.stack.imgur.com/Igqui.png
这是因为在函数调用中将输出作为int传递,而我将其声明为字符串。