【问题标题】:How do i overload operator "[]" for vector containing my own class objects如何为包含我自己的类对象的向量重载运算符“[]”
【发布时间】:2019-04-18 09:02:17
【问题描述】:

所以我得到了一个类 WayPoint(在命名空间 HHN 内)。 我有一个类 WayPointContainer。 容器有一个私有向量变量来存储“HHN::WayPoint”类型的对象

我现在想做的是我想重载 operator[] 以便我可以 像这样轻松访问向量内的对象:

WayPoint p1("name",1.5,2.0);
WayPointContainer c1; 
c1[0] = p1          // This would add the WayPoint p1 to the vector of the container on index 0
WayPoint p2 = c1[0] // This would get the WayPoint from the vector at index 0 and copy it to p2

...

我发现了不同的实现,但它们是针对其他类型的,然后是向量,或者在向量中没有使用复杂类型。

这是我的 WayPointContainer.h

#include <vector>
#include <iostream>
#include "WayPoint.h"

#ifndef SRC_WAYPOINTCONTAINER_H_
#define SRC_WAYPOINTCONTAINER_H_

class WayPointContainer {
    private:
        std::vector<HHN::WayPoint>* pContainer{ nullptr };

    public:
        WayPointContainer();
        WayPointContainer(const WayPointContainer& orig);
        virtual ~WayPointContainer();

        WayPointContainer& operator=(const WayPointContainer& rhs);
        HHN::WayPoint& operator[](int idx) const;

        void Add(const HHN::WayPoint& arg);

        int Size() const;
        void Print() const;
};

#endif /* SRC_WAYPOINTCONTAINER_H_ */

这是我的 WayPointContainer.cpp

#include <vector>
#include "WayPointContainer.h"
#include <iostream>
using namespace std;

//Default Konstruktor
WayPointContainer::WayPointContainer() {
    //Heap bereich ... new ... pContainer
    pContainer = new std::vector<HHN::WayPoint>;
}

//Destruktor
WayPointContainer::~WayPointContainer() {} //TODO

//Copy Konstruktor
WayPointContainer::WayPointContainer(const WayPointContainer& orig) {
    pContainer = orig.pContainer;
}

WayPointContainer& WayPointContainer::operator=(const WayPointContainer& rhs) {
    if(&rhs == this) {
        return *this;
    }
    if ( pContainer != rhs.pContainer) {
        pContainer = rhs.pContainer;
    }
    return *this;
}

HHN::WayPoint& WayPointContainer::operator[](int idx) const {*
    //invalid initialization of reference of type 'HHN::WayPoint&' from expression of type 'std::vector<HHN::WayPoint>'
    return pContainer[idx];
}

void WayPointContainer::Add(const HHN::WayPoint& arg) {
    pContainer->insert(pContainer->begin(), arg);
}

int WayPointContainer::Size() const {
    int i = pContainer->size();
    return i;
}

void WayPointContainer::Print() const {
    for (auto waypoint = pContainer->begin(); waypoint != pContainer->end(); ++waypoint) {
        cout << waypoint->Name();
    }
}

我正在努力的方法:

HHN::WayPoint& WayPointContainer::operator[](int idx) const {*
    //invalid initialization of reference of type 'HHN::WayPoint&' from expression of type 'std::vector<HHN::WayPoint>'
    return pContainer[idx];
}

我在那里实现的代码出现上述无效初始化错误。

所以我希望使用顶部描述的 [] 运算符,但现在它没有实现或实现时出现错误。

(我还缺少 WayPointContainer 的析构函数中向量“pContainer”的析构函数。因此,如果您知道,请随时添加它,但这不是我的问题,只是一个奖励。)

如果你愿意,我还可以提供我为 WayPoint 类和我用来测试它的 main.cpp 获得的代码。

【问题讨论】:

    标签: c++ vector operator-overloading


    【解决方案1】:

    错误消息非常清楚地表明您的操作员实施中的直接问题

    invalid initialization of reference of type 'HHN::WayPoint&' from expression of type 'std::vector<HHN::WayPoint>'
    

    pContainer[idx]idx 的偏移量取消引用pContainer,因此结果是std::vector&lt;HHN::WayPoint&gt; 类型。

    解决问题的方法有两种:

    1. 您可以取消引用指针并在其上应用idx

      return (*pContainer)[idx];
      
    2. 您根本不使用指针来保存您的 std::vector&lt;HHN::WayPoint&gt; 类成员(推荐的解决方案):

      class WayPointContainer {
           private:
               std::vector<HHN::WayPoint> container;
           // ...
      };
      

      在这种情况下,您不需要处理指针的内存取消/分配,并且可以简单地将您的运算符重载写为

      HHN::WayPoint& WayPointContainer::operator[](int idx) const {
           return container[idx];
      }
      

    【讨论】:

    • 嗯,谢谢,我想我知道它可能非常明显,但我刚开始使用 C++ 并无法弄清楚。我花了 10 多个小时才走到这一步。我仍然把指针和引用弄乱了很多。头文件是给我的,所以我不想更改它们。
    • @Saturas “头文件是给我的,所以我不想更改它们。”好吧,如果它们是你的老师给你的,他们必须要么无能,要么只是虐待狂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多