c++ – No matching member function for call to ‘push_back’ error
c++ – No matching member function for call to ‘push_back’ error
//Add_ship method bool
fleet::add_ship(ship const & s)
{
ships.push_back(&s); (No matching member function for call to 'push_back')
return true;
}
The error is because of the declaration:
std::vector<ship*> ships;
The vector contains pointers to mutable ships, but the code passes a pointer to a const ship to push_back
. You either need to store const pointers in the vector:
std::vector<const ship*> ships;
Or pass a non const pointer to the push_back:
fleet::add_ship(ship & s)
{
ships.push_back(&s); (No matching member function for call to 'push_back')
return true;
}
Side note: move the above function to a cpp, move it to the body of the class, or declare/define it as inline, if you don’t want to get linker errors.
c++ – No matching member function for call to ‘push_back’ error
Related posts on c++ :
- c++ – InterlockedIncrement usage
- c++ – How do I render a triangle in QOpenGLWidget?
- visual c++ – filling up an array in c++
- loops – C++ – using glfwGetTime() for a fixed time step
- c++ – Eigen – get a matrix from a map?
- python 2.7 – Microsoft Visual C++ 9.0 is required
- c++ error: invalid types int[int] for array subscript
- c++ – no default constructor exists for class