c++ - 如何将对象添加到 vector 并检查它是否有效

标签 c++ vector

我有一个 WayPoint 类,其中包含一对 double 和一个名称。 我有一个类 WayPointContainer,其中包含一个 Waypoints vector 。

WayPointContainer 有一个方法可以将 WayPoint 添加到它的 vector 中,还有一个 Print 方法可以显示 vector 中的 WayPoint。我都实现了,但不知道如何检查它是否有效。我的打印方法不打印任何 WayPointName。所以我的Add 方法或我的Print 方法是错误的。

因此,只要我调用 WayPointContainer 的添加方法 c1->Add(*p1),程序就会停止。 我尝试使用带有迭代器的 vector 的 insert() 来使用不同的版本。我也尝试打印容量,但每种方法都会导致程序停止,所以我猜初始化有问题? 我从我的教授那里得到了头文件,所以我希望它们是正确的。因为他没有回答我,所以我在这里试试。

我试着调试它,但我发现调试给出的信息真的很难阅读和使用。我也尝试了这些方法的不同实现,但结果总是一样。

路点.h

#ifndef WAYPOINT_H_
#define WAYPOINT_H_
#include <iostream>

namespace HHN {

class WayPoint {
private:
    std::string name{ "" };
    std::pair<double, double> coords{ 0.0, 0.0 };
public:
    WayPoint();
    virtual ~WayPoint();
    WayPoint(const WayPoint& orig);
    WayPoint(const std::string& xName, double xCoord, double yCoord);

    WayPoint& operator=(const WayPoint& rhs);

    std::string Name() const;
    double first() const;
    double second() const;
};

} /* namespace HHN */

#endif /* WAYPOINT_H_ */

路点.cpp

#include "WayPoint.h"

namespace HHN {

WayPoint::WayPoint() {}

WayPoint::~WayPoint() {}

WayPoint::WayPoint(const WayPoint& orig) {}

WayPoint::WayPoint(const std::string& xName, double xCoord, double yCoord) {
    this->name = xName;
    this->coords.first = xCoord;
    this->coords.second = yCoord;
}

WayPoint& WayPoint::operator =(const WayPoint& rhs) {
    if (this == &rhs) {
        return *this;
    }
    else {
        this->coords.first = rhs.first();
        this->coords.second = rhs.second();
        this->name = rhs.Name();
    }
    return *this;
}

std::string WayPoint::Name() const {
    return name;
}

double WayPoint::first() const {
    return this->coords.first;
}

double WayPoint::second() const {
    return this->coords.second;
}

} /* namespace HHN */

WayPointContainer.h

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

#ifndef WAYPOINTCONTAINER_H_
#define 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 /* WAYPOINTCONTAINER_H_ */

路点容器.cpp

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

WayPointContainer::WayPointContainer() {
    pContainer = new std::vector<HHN::WayPoint>;
}

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

WayPointContainer::~WayPointContainer() {
    delete[] pContainer;
}

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

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

void WayPointContainer::Add(const HHN::WayPoint& arg) {
    this->pContainer->push_back(arg);
}

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

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

main.cpp

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

int main() {
    cout << "!!!Hello World!!!" << endl;

    HHN::WayPoint *p1 = new HHN::WayPoint("nameOfP1",1.5,1.5);  // works
    HHN::WayPoint *p2(p1);                              // works

    WayPointContainer *c1 = new WayPointContainer();    // works
    WayPointContainer *c2(c1);                          // works
    WayPointContainer *c3 = new WayPointContainer();

    c1->Add(*p1);
    c1->Print();

    cout << " Punkt1: " << p1->Name() << " "<< p1->first() << " " << p1->second() << "\n";

    cout << "!!!Hello World out!!!" << endl;
    return 0;
}

我得到的控制台输出是:

!!!Hello World!!!
 <- Name
 Punkt1: nameOfP1 1.5 1.5
!!!Hello World out!!!

但我很期待

!!!Hello World!!!
 nameOfP1 <- Name
 Punkt1: nameOfP1 1.5 1.5
!!!Hello World out!!!

(这实际上是我教授的一项任务,用于实现给定的头文件。由于到目前为止他忽略了我的邮件,所以我在这里尝试。我很确定这是一个非常小的问题,可能很容易修复,但我非常 C++ 的初学者并且很挣扎。我已经坐在那个实现上好几天了。)

最佳答案

您正在将 WayPoint 的拷贝插入到容器中,但您错过了实现复制构造函数。

void WayPointContainer::Add(const HHN::WayPoint& arg) {
// push_pack do copy object
    this->pContainer->push_back(arg);
}
WayPoint::WayPoint(const WayPoint& orig) {}

尝试实现复制构造函数。

WayPoint::WayPoint(const WayPoint& orig) {
    name = orig.name;
    coords.first = orig.coords.first;
    coords.second = orig.coords.second;
}

关于c++ - 如何将对象添加到 vector 并检查它是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55793062/

相关文章:

python - 具有与 Python 的过滤器和映射相同功能的 C++ 工具

c++ - if-cases 泄漏中定义的变量? (也就是为什么这甚至可以编译?)

c++ - C++以相同方式排列两个 vector

python - 从python向量中随机选取两个大于零的值

C++ vector 问题

c++ - vector 初始化期间出现未知应用程序故障

android - 如何在android中使用矢量drawable(svg)作为复选框和单选按钮上的按钮

c++ - 在 windows 和 mac 上运行的 Opengl cpp 代码?

c++ - 是否有从其他模板参数推导出来的强制模板参数之类的东西?

c++ - vector 返回负大小 C++