C++ 如何将对象添加到向量并检查其是否有效
我得到了一个类航路点,它包含一对双精度和一个名称。 我得到了一个类WayPointContainer,它包含一个Waypoints向量 WayPointContainer获得了一种将航路点添加到其矢量的方法和一种显示矢量内航路点的打印方法。我两个都实现了,但不知道如何检查它是否有效。我的打印方法不打印任何WayPointName。因此,要么我的添加方法,要么我的打印方法是错误的 因此,只要我调用WayPointContainer的Add方法c1->Add(*p1),程序就会停止。 我尝试使用带迭代器的向量的insert()创建不同的版本。我也尝试打印容量,但每种方法都会导致程序停止,所以我猜初始化有问题吗? 我从我的教授那里得到了头文件,所以我希望它们是正确的。既然他不回答我,我就在这里试试 我试着调试它,但我发现调试提供的信息很难阅读和使用。我也尝试了不同的方法实现,但结果总是一样的 航路点C++ 如何将对象添加到向量并检查其是否有效,c++,vector,C++,Vector,我得到了一个类航路点,它包含一对双精度和一个名称。 我得到了一个类WayPointContainer,它包含一个Waypoints向量 WayPointContainer获得了一种将航路点添加到其矢量的方法和一种显示矢量内航路点的打印方法。我两个都实现了,但不知道如何检查它是否有效。我的打印方法不打印任何WayPointName。因此,要么我的添加方法,要么我的打印方法是错误的 因此,只要我调用WayPointContainer的Add方法c1->Add(*p1),程序就会停止。 我尝试使用带迭
#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_ */
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_ */
#包括
#包括
#包括“航路点.h”
#ifndef航路点集装箱_
#定义航路点容器_
类航路点集装箱{
私人:
std::vector*pContainer{nullptr};
公众:
WayPointContainer();
WayPointContainer(常量WayPointContainer&orig);
虚拟~WayPointContainer();
WayPointContainer和operator=(常数WayPointContainer和rhs);
HHN::航路点和操作员[](国际idx)常数;
无效添加(常数HHN::航路点和参数);
int Size()常量;
无效打印()常量;
};
#endif/*航路点集装箱*/
WayPointContainer.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";
}
}
#包括“WayPointContainer.h”
#包括
#包括
使用名称空间std;
WayPointContainer::WayPointContainer(){
pContainer=新标准::向量;
}
WayPointContainer::WayPointContainer(常量WayPointContainer&orig){
pContainer=orig.pContainer;
}
WayPointContainer::~WayPointContainer(){
删除[]pContainer;
}
WayPointContainer和WayPointContainer::operator=(常量WayPointContainer和rhs){
如果(此==&rhs){
归还*这个;
}
否则{
pContainer=rhs.pContainer;
}
归还*这个;
}
HHN::航路点和航路点容器::运算符[](int idx)常量{
返回(*pContainer)[idx];
}
无效航路点容器::添加(常数HHN::航路点和参数){
此->pContainer->推回(arg);
}
int WayPointContainer::Size()常量{
返回pContainer->size();
}
void WayPointContainer::Print()常量{
对于(自动航路点=pContainer->begin();航路点!=pContainer->end();+航路点){
自动tmp=航路点->名称();
cout您正在将航路点的副本插入到容器中,但是您没有实现副本构造函数
void WayPointContainer::Add(const HHN::WayPoint& arg) {
// push_pack do copy object
this->pContainer->push_back(arg);
}
WayPoint::WayPoint(const WayPoint& orig) {
name = orig.name;
coords.first = orig.coords.first;
coords.second = orig.coords.second;
}
尝试实现复制构造函数
void WayPointContainer::Add(const HHN::WayPoint& arg) {
// push_pack do copy object
this->pContainer->push_back(arg);
}
WayPoint::WayPoint(const WayPoint& orig) {
name = orig.name;
coords.first = orig.coords.first;
coords.second = orig.coords.second;
}
您正在将航路点的副本插入容器中,但未实现副本构造函数
void WayPointContainer::Add(const HHN::WayPoint& arg) {
// push_pack do copy object
this->pContainer->push_back(arg);
}
WayPoint::WayPoint(const WayPoint& orig) {
name = orig.name;
coords.first = orig.coords.first;
coords.second = orig.coords.second;
}
尝试实现复制构造函数
void WayPointContainer::Add(const HHN::WayPoint& arg) {
// push_pack do copy object
this->pContainer->push_back(arg);
}
WayPoint::WayPoint(const WayPoint& orig) {
name = orig.name;
coords.first = orig.coords.first;
coords.second = orig.coords.second;
}
您应该看看。作为std::vector指针的pContainer
成员变量不是一个好的设计。这导致了几个错误。您的复制构造函数和复制赋值运算符将使两个容器都指向同一个std::vector。我想您不希望一个容器的更改同时出现在这两个容器中。这也会导致错误ad可以在错误被销毁时双重删除错误。当应该删除时,析构函数使用delete[]
。我的建议是停止使用指针和new
。这些指针在代码中任何地方都不需要,只会导致错误和内存泄漏。只是一个小更新。我打印了指针指向的位置(十六进制值)来比较对象的向量,我意识到我的WayPointContainer.cpp的复制构造函数是错误的。通过这个实现,它可以工作:pContainer=new std::vector(*orig.pContainer)
您应该看一看。pContainer
成员变量作为std::vector的指针不是一个好的设计。这导致了几个错误。您的复制构造函数和复制赋值运算符将使两个容器都指向同一个std::vector。我认为您不希望一个容器的更改同时出现在这两个容器中。这将销毁时还会导致双重删除错误。析构函数使用delete[]
当它应该是删除时
。我的建议是停止使用指针和新建
。这些指针在代码中的任何地方都不需要,只会导致错误和内存泄漏。只是一个小的更新。我打印了指针指向的位置(十六进制值)为了比较对象的向量,我意识到我的WayPointContainer.cpp的复制构造函数是错误的。通过这个实现,它可以工作:pContainer=new std::vector(*orig.pContainer)
你是对的,谢谢,我完全忽略了这一点。现在我只需要正确地获取析构函数部分,因为如果我删除C1的pccontainer,而C2=C1,那么C2的pccontainer也将被删除。你是对的,谢谢,我完全忽略了这一点。现在我只需要正确地获取析构函数部分,因为如果我删除C1的pccontainer,而C2=C1,那么pC2的容器也将被删除。