I'm trying to overload the []
operator (similar to std::vector::operator[]
) for a list
containing shared_ptr
. It needs to return a reference to an element at position index
(design specs I've been given).
Classes car
and truck
are derived from abstract base class vehicle
.
Class dealership
contains a std::list<std::shared_ptr<vehicle>> dealershipLot;
This is how I've been trying to overload the []
operator;
std::list<std::shared_ptr<vehicle>>& Dealership::operator[](size_t index)
I tried using std::find
to get an iterator to the element position and return a reference using &(findIter)
but it seems std::find
needs an overloaded ==
to function with my list type, but I get the error
binary ==: no operator found which takes a left-hand operand of type std::shared_ptr<vehicle> (or there is no acceptable conversion)
The following is a shortened version of my code:
#include <vector>
#include <iostream>
#include <list>
#include <string>
#include <algorithm>
#include <memory>
#include <fstream>
using namespace std;
class vehicle {
protected:
string name;
public:
vehicle(){ name.clear(); }
vehicle(string v) : name(v){};
string getName() const { return name; };
virtual void display(ostream&) const = 0;
};
class Car : public vehicle {
protected:
int no;
public:
Car(){ no = 0; };
Car(string n, int no) : vehicle(n), no(no) {};
std::string getName() const { return name; }
int getNo() const{ return no; }
void display(ostream& os) const {
os << name << " " << no << std::endl;
}
};
class Truck : public vehicle {
protected:
int no;
int km;
public:
Truck(){ no = 0; };
Truck(string n, int no, int km) : vehicle(n), no(no), km(km) {};
std::string getName() const { return name; }
int getNo() const{ return no; }
int getKm() const{ return km; }
void display(ostream& os) const {
os << name << " " << no << "" << km << std::endl;
}
};
class Dealership{
string dealershipName;
std::list<std::shared_ptr<vehicle>> dealershipLot;
public:
Dealership(){ dealershipName.clear(); dealershipLot.clear(); };
Dealership(const std::string n);
Dealership(const Dealership&); //Copy constructor
Dealership& operator=(const Dealership&); //Copy assignment operator
Dealership(Dealership&&); //Move constructor
Dealership&& operator=(Dealership&&); //Move assignment operator
void operator+=(std::shared_ptr<vehicle> veh); //Operator += overload
bool operator==(const std::shared_ptr<vehicle> other){ //???
return dealershipName == other->getName();
}
std::list<std::shared_ptr<vehicle>>& operator[](size_t index){ //???
/*size_t index = 3;
std::list<std::shared_ptr<vehicle>>::iterator findIter =
std::find(dealershipLot.begin(), dealershipLot.end(), index);
cout << &(findIter) << endl;
return &(findIter);*/
}
};
int main()
{
Dealership d1("lot3");
d1 += std::move(std::shared_ptr<vehicle>(new Car("Toyota", 15)));
return 0;
}
How do I overload the []
operator to get a reference to the list
element?
Edit:
For anyone interested I overloaded []
as;
//Operator [] return reference to element at position n
vehicle& Task::operator[](size_t index){
std::list<std::shared_ptr<vehicle>>::iterator it = dealershipLot.begin();
std::advance(it, index);
return **it;
}
I created an iterator that advances through the dealershipLot
list index
positions.Then returned the object that the iterator is pointing to as a reference.