. Advertisement .
..3..
. Advertisement .
..4..
I’m building a new program, but when I run it, an error pops up. The error displayed is as follows:
No matching member function for call 'push_back'
I have tried several workarounds, but they still do not get the desired results. If you have come across this situation and have a solution for the “no matching member function for call to ‘push_back’” problem, please let me know. Here is what I do:
//fleet.h
#include "ship.h"
#include <vector>
#include <iostream>
#ifndef fleet_h
#define fleet_h
using namespace std;
class fleet
{
public:
//Add_ship and remove_ship method
bool add_ship(ship const &s);
private:
vector<ship*> ships;
};
//Add_ship method
bool fleet::add_ship(ship const & s){
ships.push_back(&s);
return true;
}
#endif /* fleet_h */
Thanks!
The cause:
The reason for the error is the declaration:
Solution:
You should store const pointers in the vector in this way:
Another way: moves a non const pointer to push_back: