std::list 包含 shared_ptr 的 C++ 重载 []

C++ Overloading [] for std::list containing shared_ptr

我正在尝试为包含 shared_ptrlist 重载 [] 运算符(类似于 std::vector::operator[])。它需要 return 对位置 index 的元素的引用(我已经给出了设计规范)。

Classes cartruck 派生自抽象基础 class vehicle。 Class dealership 包含一个 std::list<std::shared_ptr<vehicle>> dealershipLot;

这就是我一直试图重载 [] 运算符的方式; std::list<std::shared_ptr<vehicle>>& Dealership::operator[](size_t index)

我尝试使用 std::find 获取元素位置的迭代器,并使用 &(findIter) return 获取引用,但似乎 std::find 需要重载 == 与我的列表类型一起工作,但我收到错误

binary ==: no operator found which takes a left-hand operand of type std::shared_ptr<vehicle> (or there is no acceptable conversion)

以下是我的代码的简化版本:

#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;
}

如何重载 [] 运算符以获取对 list 元素的引用?

编辑: 对于任何感兴趣的人,我将 [] 重载为;

//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;
    }

我创建了一个遍历 dealershipLot 列表的迭代器 index positions.Then 返回了迭代器指向的对象作为参考。