简体   繁体   English

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

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

I'm trying to overload the [] operator (similar to std::vector::operator[] ) for a list containing shared_ptr . 我正在尝试为包含shared_ptrlist重载[]运算符(类似于std::vector::operator[] )。 It needs to return a reference to an element at position index (design specs I've been given). 它需要在位置index处返回对元素的引用(我已经给出了设计规格)。

Classes car and truck are derived from abstract base class vehicle . cartruck类别来源于抽象的基本类别的vehicle Class dealership contains a std::list<std::shared_ptr<vehicle>> dealershipLot; dealership包含一个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 我尝试使用std::find来获取元素位置的迭代器,并使用&(findIter)返回引用&(findIter)但似乎std::find需要重载==才能与列表类型一起工作,但出现错误

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? 如何重载[]运算符以获取对list元素的引用?

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. 我创建了一个迭代器,该迭代器可通过dealershipLot列表index位置前进,然后返回该迭代器指向的对象作为参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM