C++ -调用“push_back”时没有匹配的成员函数

rmbxnbpk  于 2023-04-01  发布在  其他
关注(0)|答案(3)|浏览(345)

我目前正在做一个大学作业,现在我正在努力与向量。
我应该返回一个对象的唯一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一样。谢谢你的建议!

d7v8vwbk

d7v8vwbk1#

方法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*
gojuced7

gojuced72#

vertex_puller是一个std::make_unique<VertexPuller>函数。它不是一个unique_ptr<VertexPuller>。你必须调用这个函数,并传递任何你要传递给VertexPuller构造函数的参数。

auto vertex_puller= std::make_unique<VertexPuller>(); // note the parentheses
46scxncf

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传递,而我将其声明为字符串。

相关问题