我有一个WayPoint类,其中包含一对双打和一个名字。 我有一个WayPointContainer类,其中包含Waypoint的向量。
WayPointContainer获得了一种向其矢量添加WayPoint的方法,以及一种用于在矢量内部显示WayPoint的Print方法。我都实现了,但不知道如何检查它是否有效。我的打印方法不打印任何WayPointName。因此,我的 Add 方法或 Print 方法都是错误的。
因此,一旦我调用WayPointContainer的Add方法 c1-> Add(* p1),程序便会停止。 我尝试使用带有迭代器的vector的insert()实现不同的版本。我也尝试打印容量,但是每种方法都只会导致程序停止,所以我猜初始化有问题吗? 我从教授那里获得了头文件,因此我希望它们是正确的。而且因为他没有回答我,所以我在这里尝试。
我尝试调试它,但是我发现调试给出的信息确实很难阅读和使用。我还尝试了这些方法的不同实现,但结果始终相同。
WayPoint.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_ */
WayPoint.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_ */
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";
}
}
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 ++的初学者,并且非常奋斗。我已经在该实现上坐了好几天了。)
答案 0 :(得分:2)
您要将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;
}