I am currently working on a college assignment and right now I'm struggling with vectors.
I am supposed to return a unique ID for an object and then add that object to a vector.
The object is a struct defined as follows:
struct VertexPuller{
std::vector<InVertex> head_settings;
std::vector<IndexType> indexing;
};
and the vector I am trying to push to is:
std::vector<std::unique_ptr<VertexPuller>> vertex_puller_tables;
The function I wrote looks like this:
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;
However at the second-to-last line, when i try to push the vertex puller into the vector, I get the error - No matching member function for call to 'push_back'.
I've been stuck on this for quite some time and i have no idea what may cause this, probably pointers, as usual with C and me.
Thanks for the suggestions!
The method push_back is there. The type that you send probably doesn't match. Try to read the compilation error and figure out what type was expected and what was the actual type that was sent.
A more simple example with same error:
int main() {
std::vector<int> vec;
vec.push_back("hey");
}
Compilation error is:
error: no matching function for call to `push_back`
However, if we read further it says:
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 is a std::make_unique<VertexPuller> function. It's not a unique_ptr<VertexPuller>. You have to call the function and pass any parameters you would pass to a VertexPuller constructor.
auto vertex_puller= std::make_unique<VertexPuller>(); // note the parentheses
Related
I am trying to achieve the following
#include<iostream>
#include <vector>
class var {
public:
static std::vector<var*> variables_;
friend var operator-(const var& v) {
// this is not compiling
variables_.push_back(&v);
}
};
int main() { var x; }
The error
est.cpp: In function ‘var operator-(const var&)’:
test.cpp:10:32: error: no matching function for call to ‘push_back(const var*)’
10 | variables_.push_back(&v);
| ^
In file included from /usr/include/c++/10/vector:67,
from test.cpp:2:
/usr/include/c++/10/bits/stl_vector.h:1187:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = var*; _Alloc = std::allocator<var*>; std::vector<_Tp, _Alloc>::value_type = var*]’ (near match)
1187 | push_back(const value_type& __x)
| ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1187:7: note: conversion of argument 1 would be ill-formed:
test.cpp:10:30: error: invalid conversion from ‘const var*’ to ‘std::vector<var*>::value_type’ {aka ‘var*’} [-fpermissive]
10 | variables_.push_back(&v);
| ^~
| |
| const var*
In file included from /usr/include/c++/10/vector:67,
from test.cpp:2:
/usr/include/c++/10/bits/stl_vector.h:1203:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = var*; _Alloc = std::allocator<var*>; std::vector<_Tp, _Alloc>::value_type = var*]’ (near match)
1203 | push_back(value_type&& __x)
| ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1203:7: note: conversion of argument 1 would be ill-formed:
test.cpp:10:30: error: invalid conversion from ‘const var*’ to ‘std::vector<var*>::value_type’ {aka ‘var*’} [-fpermissive]
10 | variables_.push_back(&v);
| ^~
| |
| const var*
test.cpp:11:5: warning: no return statement in function returning non-void [-Wreturn-type]
11 | }
| ^
I have read several posts on this site telling me why this wont work. One of which is that elements of the vector have to be copy assignable. So I understand WHY, but I don't understand how to get around it.
There must be a way around this.
You take a const var&, but your vector stores var* (without the const). Either your vector needs to store pointers to constants
static std::vector<const var*> variables_;
or your operator- needs to take a non-constant reference
friend var operator-(var& v) { ... }
You should do the first thing if you never plan to modify var through the variables_ vector, or the second otherwise.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Im new to cpp vectors and i keep encountering this issue
error: no matching function for call to ‘push_back(const char [6])’
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> names;
names.push_back("Lewis");
names.push_back("Mark");
names.push_back("Reece");
names.push_back("Max");
for(int i = 0; i < names.size(); i++)
cout << names[i] << '\n';
return 0;
}
This is the error
test.cpp: In function ‘int main()’:
test.cpp:6:26: error: no matching function for call to ‘push_back(const char [6])’
6 | names.push_back("Lewis");
| ^
In file included from /usr/include/c++/9/vector:67,
from test.cpp:1:
/usr/include/c++/9/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/include/c++/9/bits/stl_vector.h:1184:7: note: conversion of argument 1 would be ill-formed:
test.cpp:6:19: error: invalid conversion from ‘const char*’ to ‘std::vector<int>::value_type’ {aka ‘int’} [-fpermissive]
6 | names.push_back("Lewis");
| ^~~~~~~
| |
| const char*
In file included from /usr/include/c++/9/vector:67,
from test.cpp:1:
/usr/include/c++/9/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/include/c++/9/bits/stl_vector.h:1200:7: note: conversion of argument 1 would be ill-formed:
test.cpp:6:19: error: invalid conversion from ‘const char*’ to ‘std::vector<int>::value_type’ {aka ‘int’} [-fpermissive]
6 | names.push_back("Lewis");
| ^~~~~~~
| |
| const char*
test.cpp:7:25: error: no matching function for call to ‘push_back(const char [5])’
The issue is that you are using a vector<int> rather than a vector<string>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<string> names;
names.push_back("Lewis");
names.push_back("Mark");
names.push_back("Reece");
names.push_back("Max");
for(int i = 0; i < names.size(); i++)
cout << names[i] << '\n';
return 0;
}
I am trying to remove the first value of a vector of template typenames. Using the erase() method in vector stl, I keep getting this error:
TTrie.inc:56:19: error: passing ‘const std::vector<char, std::allocator<char> >’ as ‘this’ argument discards qualifiers [-fpermissive]
56 | sequence.erase(it);
| ~~~~~~~~~~~~~~^~~~
In file included from /usr/include/c++/10/vector:67,
from TTrie.h:5,
from TTrieTest.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:1430:7: note: in call to ‘std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::const_iterator) [with _Tp = char; _Alloc = std::allocator<char>; std::vector<_Tp, _Alloc>::iterator = std::vector<char, std::allocator<char> >::iterator; std::vector<_Tp, _Alloc>::const_iterator = std::vector<char, std::allocator<char> >::const_iterator]’
1430 | erase(const_iterator __position)
| ^~~~~
In file included from TTrie.h:115,
from TTrieTest.cpp:1:
TTrie.inc: In instantiation of ‘TTrie<DataType>& TTrie<DataType>::operator+=(const std::vector<DataType>&) [with DataType = std::__cxx11::basic_string<char>]’:
TTrieTest.cpp:93:10: required from here
TTrie.inc:56:19: error: passing ‘const std::vector<std::__cxx11::basic_string<char> >’ as ‘this’ argument discards qualifiers [-fpermissive]
56 | sequence.erase(it);
| ~~~~~~~~~~~~~~^~~~
In file included from /usr/include/c++/10/vector:67,
from TTrie.h:5,
from TTrieTest.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:1430:7: note: in call to ‘std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::const_iterator) [with _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::iterator = std::vector<std::__cxx11::basic_string<char> >::iterator; std::vector<_Tp, _Alloc>::const_iterator = std::vector<std::__cxx11::basic_string<char> >::const_iterator]’
1430 | erase(const_iterator __position)
| ^~~~~
In file included from TTrie.h:115,
from TTrieTest.cpp:1:
Is it having trouble because sequence should be const? Am I declaring the iterator wrong somehow? Here is my code. Please let me know If you have any questions:
template<typename DataType>
TTrie<DataType>& TTrie<DataType>::operator+=(const std::vector<DataType>& sequence) {
const DataType c = sequence[0];
const TTrie<DataType>* child = getChild(c); //Returns a pointer to a child node or a nullptr
if (!child) {
TTrie<DataType>* n = new TTrie<DataType>(c);
edgeMap[c] = n;
}
if(sequence.size() > 1) {
typename std::vector<DataType>::const_iterator it;
it = sequence.cbegin(); //First value of sequence
sequence.erase(it);
*edgeMap[c] += sequence;
}
return *this;
}
Your sequence parameter is a reference to a const std::vector<DataType> object. But std::vector::erase() is not qualified as being a const method, as it modifies the content of the vector, so it cannot be called on a const object. That is what the error message is trying to tell you - that erase() is being called on a const object.
I've been trying to get my artificial intelligence program (written in C++11) to stop spewing out error messages larger than my Terminal will record. I've withered away methods one at a time until the message went away, and have hence found the precise line which makes the computer go bonkers. Here's the pertinent part of my code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
class H
{
public:
H(int);
int get_val();
private:
int val;
};
class Hvote
{
public:
Hvote() : error(1.0) { }
std::vector<H&> hs;
double error;
};
int main()
{
Hvote hvote;
return 0;
}
To me, it all looks reasonable. However, the compiler (g++) disagrees. The error messages are so long that my Terminal doesn't even bother saving the beginning, so I don't know their true length. However, they're pretty repetitive. The last page, for instance, reads:
/usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘class std::vector<H&>’:
hvote.h:16:25: required from here
/usr/include/c++/4.8/bits/stl_vector.h:237:20: error: no members matching ‘std::vector<H&>::_Base {aka std::_Vector_base<H&, std::allocator<H&> >}::_M_allocate’ in ‘std::vector<H&>::_Base {aka struct std::_Vector_base<H&, std::allocator<H&> >}’
using _Base::_M_allocate;
^
/usr/include/c++/4.8/bits/stl_vector.h:238:20: error: no members matching ‘std::vector<H&>::_Base {aka std::_Vector_base<H&, std::allocator<H&> >}::_M_deallocate’ in ‘std::vector<H&>::_Base {aka struct std::_Vector_base<H&, std::allocator<H&> >}’
using _Base::_M_deallocate;
^
/usr/include/c++/4.8/bits/stl_vector.h:878:7: error: forming pointer to reference type ‘H&’
data() _GLIBCXX_NOEXCEPT
^
/usr/include/c++/4.8/bits/stl_vector.h:886:7: error: forming pointer to reference type ‘H&’
data() const _GLIBCXX_NOEXCEPT
^
/usr/include/c++/4.8/bits/stl_vector.h:919:7: error: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = H&; _Alloc = std::allocator<H&>; std::vector<_Tp, _Alloc>::value_type = H&]’ cannot be overloaded
push_back(value_type&& __x)
^
/usr/include/c++/4.8/bits/stl_vector.h:901:7: error: with ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = H&; _Alloc = std::allocator<H&>; std::vector<_Tp, _Alloc>::value_type = H&]’
push_back(const value_type& __x)
^
/usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = H&; _Alloc = std::allocator<H&>]’:
hvote.h:10:7: required from here
/usr/include/c++/4.8/bits/stl_vector.h:416:30: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_start’
_M_get_Tp_allocator()); }
^
/usr/include/c++/4.8/bits/stl_vector.h:416:30: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_finish’
/usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = H&; _Alloc = std::allocator<H&>]’:
/usr/include/c++/4.8/bits/stl_vector.h:416:33: required from ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = H&; _Alloc = std::allocator<H&>]’
hvote.h:10:7: required from here
/usr/include/c++/4.8/bits/stl_vector.h:161:33: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_start’
- this->_M_impl._M_start); }
^
/usr/include/c++/4.8/bits/stl_vector.h:161:9: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_start’
- this->_M_impl._M_start); }
^
/usr/include/c++/4.8/bits/stl_vector.h:161:9: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_end_of_storage’
/usr/include/c++/4.8/bits/stl_vector.h:161:33: error: ‘_M_deallocate’ was not declared in this scope
- this->_M_impl._M_start); }
^
However, there's only one little change I need to make so that this all goes away. In hvote.h, just delete the line, std::vector<H&> hs;. Now everything works perfectly! What's the problem with that line? (In my not-reduced and much longer original program, hs is a variable I need, as opposed to here where I've paired away all the methods of hvote that use it)
Thanks!
It has a problem with you keeping a vector of non-const references
std::vector<H&> hs;
I would suggest maintaining either a vector of values or pointers, depending on if you want to own these H instances or just reference them.
std::vector<H> hs;
std::vector<H*> hs;
class Foo {
vector<Bar> bars;
public:
Foo(int barcount) : { bars(barcount, 0); };
};
I'm trying to turn the bars vector into a vector holding barcount Bars. However I get 2 errors when doing this:
Foo.cpp:8: error: expected identifier before ‘{’ token
Foo(int barcount) : { bars(barcount, 0); };
^
Foo.cpp:8: error: no match for call to ‘(std::vector<Bar>) (int&, int)’
Foo(int barcount) : { bars(barcount, 0); };
Any help on what might be going wrong would be much appreciated.
EDIT: This is my code now (Foo/Bar replaced):
class Machine {
vector<Rotor> rotors;
public:
Machine(int rotorcount) : rotors(rotorcount, 0) {}
};
And I get a pretty long error message:
/usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_Integer, _Integer, std::__true_type) [with _Integer = int; _Tp = Rotor; _Alloc = std::allocator<Rotor>]’:
/usr/include/c++/4.8/bits/stl_vector.h:404:55: required from ‘std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = int; _Tp = Rotor; _Alloc = std::allocator<Rotor>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Rotor>]’
Machine.cpp:8:51: required from here
/usr/include/c++/4.8/bits/stl_vector.h:1166:59: error: no matching function for call to ‘std::vector<Rotor>::_M_fill_initialize(std::vector<Rotor>::size_type, int&)’
_M_fill_initialize(static_cast<size_type>(__n), __value);
^
/usr/include/c++/4.8/bits/stl_vector.h:1166:59: note: candidate is:
/usr/include/c++/4.8/bits/stl_vector.h:1212:7: note: void std::vector<_Tp, _Alloc>::_M_fill_initialize(std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = Rotor; _Alloc = std::allocator<Rotor>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = Rotor]
_M_fill_initialize(size_type __n, const value_type& __value)
^
/usr/include/c++/4.8/bits/stl_vector.h:1212:7: note: no known conversion for argument 2 from ‘int’ to ‘const value_type& {aka const Rotor&}’
The initializer list goes after the comma, but before the constructor body, and is not followed by a semicolon:
Foo(int barcount) : bars(barcount, 0) {};
no known conversion for argument 2 from ‘int’ to ‘const value_type& {aka const Rotor&}’
Sounds like you don't have a constructor that takes int as a single parameter.