[我目前正在做大学作业,现在我正在努力寻找媒介。
我应该返回一个对象的唯一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;
但是在倒数第二行,当我尝试将顶点拉子推入向量时,出现错误-没有匹配的成员函数来调用'push_back'。
我已经坚持了很长时间,我不知道是什么原因导致了这种情况,可能是指针,就像C和我一样。感谢您的建议!
方法push_back
在这里。您发送的类型可能不匹配。尝试读取编译错误并找出期望的类型以及发送的实际类型。
具有相同错误的更简单示例:
int main() {
std::vector<int> vec;
vec.push_back("hey");
}
编译错误是:
error: no matching function for call to `push_back`
但是,如果我们进一步阅读它说:
main.cpp:6:24: error: no matching function for call to 'push_back(const char [4])'
6 | vec.push_back("hey");
| ^
In file included from /usr/local/include/c++/9.2.0/vector:67,
from main.cpp:2:
/usr/local/include/c++/9.2.0/bits/stl_vector.h:1184:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int]' <near match>
1184 | push_back(const value_type& __x)
| ^~~~~~~~~
/usr/local/include/c++/9.2.0/bits/stl_vector.h:1184:7: note: conversion of argument 1 would be ill-formed:
main.cpp:6:19: error: invalid conversion from 'const char*' to 'std::vector<int>::value_type' {aka 'int'} [-fpermissive]
6 | vec.push_back("hey");
| ^~~~~
| |
| const char*
In file included from /usr/local/include/c++/9.2.0/vector:67,
from main.cpp:2:
/usr/local/include/c++/9.2.0/bits/stl_vector.h:1200:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int]' <near match>
1200 | push_back(value_type&& __x)
| ^~~~~~~~~
/usr/local/include/c++/9.2.0/bits/stl_vector.h:1200:7: note: conversion of argument 1 would be ill-formed:
main.cpp:6:19: error: invalid conversion from 'const char*' to 'std::vector<int>::value_type' {aka 'int'} [-fpermissive]
6 | vec.push_back("hey");
| ^~~~~
| |
| const char*