C++ - 没有匹配的成员函数来调用 'push_back'
C++ - No matching member function for call to 'push_back'
我目前正在完成一项大学作业,现在我正在为矢量而苦苦挣扎。
我应该 return 对象的唯一 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*
vertex_puller
是一个 std::make_unique<VertexPuller>
函数。这不是 unique_ptr<VertexPuller>
。您必须调用该函数并将要传递给 VertexPuller
构造函数的任何参数传递给
auto vertex_puller= std::make_unique<VertexPuller>(); // note the parentheses
我目前正在完成一项大学作业,现在我正在为矢量而苦苦挣扎。
我应该 return 对象的唯一 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*
vertex_puller
是一个 std::make_unique<VertexPuller>
函数。这不是 unique_ptr<VertexPuller>
。您必须调用该函数并将要传递给 VertexPuller
构造函数的任何参数传递给
auto vertex_puller= std::make_unique<VertexPuller>(); // note the parentheses