2016-11-26 4 views
0

listshared_ptrの場合、[]演算子(std::vector::operator[]に似ています)にオーバーロードしようとしています。位置index(私が与えられた設計仕様)の要素への参照を返す必要があります。C++オーバーライド[] for shared_ptrを含むstd :: list

クラスcarおよびtruckは、抽象基本クラスvehicleから派生したものです。 クラスdealershipには、次のものが含まれていますstd::list<std::shared_ptr<vehicle>> dealershipLot;

これは私が[]演算子をオーバーロードしようとしてきた方法です。 std::list<std::shared_ptr<vehicle>>& Dealership::operator[](size_t index)

私は要素の位置に反復子を取得し、&(findIter)を使用して参照を返すようにstd::findを使用してみましたが、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要素への参照を取得するにはどうすればよいですか?

+1

'std :: find'はあなたが望むものではありません。 'std :: next'か' std :: advance'を試してください。 –

+1

トピックオフ: 'std :: list'の' operator [] 'は今週人気のある質問のようです。 'std :: list'はランダムアクセス用に設計されていません。クラスに合格するにはこれを行いますが、仕事でそれをする前に本当に難しいと思います。 – user4581301

+0

"リスト要素への参照を取得"したいが、演算子がリストを返す...意味がない。リスト要素への参照を返す場合は、正しい型を返すように演算子を変更する必要があります。それ以上の文脈がなければ、ディーラーと車両のための '=='演算子のオーバーロードは意味をなさない。最後に、std :: listではなく、std :: arrayまたはstd :: vectorに車両を格納しないでください。リストにインデックスを作成する場合は、正しいデータ構造(リンクリストではなく配列)を使用する必要があります。 – smac89

答えて

1

編集: 興味があれば、私は[]をオーバーロードしました。

//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はdealershipLotリストindex positions.Thenを通して前進反復子を作成し、イテレータを基準とを指しているオブジェクトが返さ。

関連する問題