mapをメンバとして持つクラスを作ったのですが、mapに自作のクラスをinsertしようとするとプログラムが落ちます。
map<int, string>などは普通にinsertできるので自作クラスに問題があるのではないかと思っていますが、原因が分かりません。
何が原因なのか、追加すべきものがあるのか、ご助力いただければと思います。
開発環境はWindows7(64bit)、VisualStudio 2012です。
作成しているプログラムは以下の通りです。
ID番号付きの2次元の点を表すCPoint2DWithIDを、
2次元の3角形を表すCTriangle2Dに頂点の情報としてmapに格納しようとしています。
コンパイルは通るのですが、プログラムを実行するとinsertのところで動作が停止します。
コードで長くなりますが、よろしくお願い致します。
main.cpp
#include <stdio.h>
#include "CTriangle2D.h"
int main() {
CPoint2DWithID *aPoint1 = new CPoint2DWithID(1, 10.0, 10.0);
CPoint2DWithID *aPoint2 = new CPoint2DWithID(7, 50.0, 10.0);
CPoint2DWithID *aPoint3 = new CPoint2DWithID(4, 10.0, 30.0);
CTriangle2D *aTriangle = new CTriangle2D(*aPoint1, *aPoint2, *aPoint3);
// ここで中のnodesにinsertできずに落ちる
return 0;
}
#ifndef INCLUDE_CTRIANGLE2D
#define INCLUDE_CTRIANGLE2D
#include <map>
#include "CPoint2DWithID.h"
class CTriangle2D {
private:
std::map<int, CPoint2DWithID> nodes;
public:
// Constructor
CTriangle2D(CPoint2DWithID cCPoint2DWithID1, CPoint2DWithID cCPoint2DWithID2, CPoint2DWithID cCPoint2DWithID3);
// Destructor
virtual ~CTriangle2D();
// Functions
bool equals(CTriangle2D cCTriangle2D);
bool isInThisTriangle(CPoint2D cCPoint2D);
bool isInThisTriangle(double cX, double cY);
// getter
std::map<int, CPoint2DWithID> getNodes() const;
};
#endif // INCLUDE_CTRIANGLE2D
#include "CTriangle2D.h"
// Constructor
CTriangle2D::CTriangle2D(CPoint2DWithID cCPoint2DWithID1, CPoint2DWithID cCPoint2DWithID2, CPoint2DWithID cCPoint2DWithID3) {
// 3角形を構成する3点をnodesに登録する
this->nodes.insert(std::map<int, CPoint2DWithID>::value_type(cCPoint2DWithID1.getID(), cCPoint2DWithID1));
this->nodes.insert(std::map<int, CPoint2DWithID>::value_type(cCPoint2DWithID2.getID(), cCPoint2DWithID2));
this->nodes.insert(std::map<int, CPoint2DWithID>::value_type(cCPoint2DWithID3.getID(), cCPoint2DWithID3));
}
// Destructor
CTriangle2D::~CTriangle2D() {
// TODO デストラクタの作成
}
// Functions
bool CTriangle2D::equals(CTriangle2D cCTriangle2D) {
// 要素の3点をそれぞれ取り出し、すべてにおいてequalsであればtrue
std::map<int, CPoint2DWithID>::iterator aIterator = this->nodes.begin();
CPoint2DWithID aThisPoint1 = (*aIterator).second; ++aIterator;
CPoint2DWithID aThisPoint2 = (*aIterator).second; ++aIterator;
CPoint2DWithID aThisPoint3 = (*aIterator).second;
aIterator = cCTriangle2D.getNodes().begin();
CPoint2DWithID aOtherPoint1 = (*aIterator).second; ++aIterator;
CPoint2DWithID aOtherPoint2 = (*aIterator).second; ++aIterator;
CPoint2DWithID aOtherPoint3 = (*aIterator).second;
bool aBool;
aBool = aThisPoint1.equals(aOtherPoint1);
if (!aBool) {
return false;
}
aBool = aThisPoint2.equals(aOtherPoint2);
if (!aBool) {
return false;
}
aBool = aThisPoint3.equals(aOtherPoint3);
if (!aBool) {
return false;
}
return true;
}
bool CTriangle2D::isInThisTriangle(CPoint2D cCPoint2D) {
bool aBool = this->isInThisTriangle(cCPoint2D.getX(), cCPoint2D.getY());
return aBool;
}
bool CTriangle2D::isInThisTriangle(double cX, double cY) {
// 格納している3点を展開
std::map<int, CPoint2DWithID>::iterator aIterator = this->nodes.begin();
CPoint2DWithID aThisPointA = (*aIterator).second; ++aIterator;
CPoint2DWithID aThisPointB = (*aIterator).second; ++aIterator;
CPoint2DWithID aThisPointC = (*aIterator).second;
// 点の値のみに分解
double aAx = aThisPointA.getPoint2D()->getX();
double aAy = aThisPointA.getPoint2D()->getY();
double aBx = aThisPointB.getPoint2D()->getX();
double aBy = aThisPointB.getPoint2D()->getY();
double aCx = aThisPointC.getPoint2D()->getX();
double aCy = aThisPointC.getPoint2D()->getY();
double aDx = cX;
double aDy = cY;
// 辺ベクトルと頂点から点Dへのベクトルでそれぞれ外積ベクトルの大きさを求める
// 外積ベクトル (A→B)×(A→D)
double aCPVectorABAD = (aBx - aAx) * (aDy - aAy) - (aDx - aAx) * (aBy - aAy);
// 外積ベクトル (B→C)×(B→D)
double aCPVectorBCBD = (aCx - aBx) * (aDy - aBy) - (aDx - aBx) * (aCy - aBy);
// 外積ベクトル (C→A)×(C→D)
double aCPVectorCACD = (aAx - aCx) * (aDy - aCy) - (aDx - aCx) * (aAy - aCy);
// 3つの外積ベクトルの値がすべて0以上、もしくは0以下ならこの三角形上に存在する
// 値の1つが0なら辺上、2つが0なら頂点上に存在する
if ((aCPVectorABAD >= 0.0 && aCPVectorBCBD >= 0.0 && aCPVectorCACD >= 0.0)
|| (aCPVectorABAD <= 0.0 && aCPVectorBCBD <= 0.0 && aCPVectorCACD <= 0.0)) {
return true;
}
return false;
}
// Getter
std::map<int, CPoint2DWithID> CTriangle2D::getNodes() const {
return this->nodes;
}
#ifndef INCLUDE_CPOINT2DWITHID
#define INCLUDE_CPOINT2DWITHID
#include "CPoint2D.h"
class CPoint2DWithID {
private:
int id;
CPoint2D *point2D;
public:
// Constructor
CPoint2DWithID();
CPoint2DWithID(int cID, CPoint2D cCPoint2D);
CPoint2DWithID(int cID, double cX, double cY);
// Destructor
~CPoint2DWithID();
// Functions
bool equals(CPoint2DWithID cCPoint2DWithID);
bool equals(int cID, CPoint2D cCPoint2D);
bool equals(int cID, double cX, double cY);
bool equals(CPoint2D cCPoint2D);
bool equals(double cX, double cY);
// Getter
int getID() const;
CPoint2D *getPoint2D() const;
// Setter
void setID(int cID);
void setPoint2D(CPoint2D cCPoint2D);
void setPoint2D(double cX, double cY);
};
#endif // INCLUDE_CPOINT2DWITHID
#include "CPoint2DWithID.h"
// Constructor
CPoint2DWithID::CPoint2DWithID() {
this->id = 0;
this->point2D = new CPoint2D();
}
CPoint2DWithID::CPoint2DWithID(int cId, CPoint2D cCPoint2D) {
this->id = cId;
this->point2D = new CPoint2D(cCPoint2D.getX(), cCPoint2D.getY());
}
CPoint2DWithID::CPoint2DWithID(int cId, double cX, double cY) {
this->id = cId;
this->point2D = new CPoint2D(cX, cY);
}
// Destructor
CPoint2DWithID::~CPoint2DWithID() {
delete this->point2D;
}
// functions
bool CPoint2DWithID::equals(CPoint2DWithID cCPoint2DWithID) {
bool aBool = this->equals(cCPoint2DWithID.getID(), *cCPoint2DWithID.getPoint2D());
return aBool;
}
bool CPoint2DWithID::equals(int cId, CPoint2D cCPoint2D) {
if (this->id != cId) {
return false;
}
bool aBool = this->point2D->equals(cCPoint2D);
return aBool;
}
bool CPoint2DWithID::equals(int cId, double cX, double cY) {
if (this->id != cId) {
return false;
}
bool aBool = this->point2D->equals(cX, cY);
return aBool;
}
bool CPoint2DWithID::equals(CPoint2D cCPoint2D) {
bool aBool = this->point2D->equals(cCPoint2D);
return aBool;
}
bool CPoint2DWithID::equals(double cX, double cY) {
bool aBool = this->point2D->equals(cX, cY);
return aBool;
}
// Getter
int CPoint2DWithID::getID() const {
return this->id;
}
CPoint2D *CPoint2DWithID::getPoint2D() const {
return this->point2D;
}
// Setter
void CPoint2DWithID::setID(int cId) {
this->id = cId;
}
void CPoint2DWithID::setPoint2D(CPoint2D cCPoint2D) {
this->point2D->setX(cCPoint2D.getX());
this->point2D->setY(cCPoint2D.getY());
}
void CPoint2DWithID::setPoint2D(double cX, double cY) {
this->point2D->setX(cX);
this->point2D->setY(cY);
}
#ifndef INCLUDE_CPOINT2D
#define INCLUDE_CPOINT2D
class CPoint2D {
private:
double x;
double y;
public:
// Constructor
CPoint2D();
CPoint2D(double cX, double cY);
// Destructor
virtual ~CPoint2D();
// Functions
bool equals(CPoint2D cCPoint2D);
bool equals(double cX, double cY);
// Getter
double getX() const;
double getY() const;
// Setter
void setX(double cX);
void setY(double cY);
};
#endif // INCLUDE_CPOINT2D
#include "CPoint2D.h"
// Constructor
CPoint2D::CPoint2D() {
this->x = 0.0;
this->y = 0.0;
}
CPoint2D::CPoint2D(double cX, double cY) {
this->x = cX;
this->y = cY;
}
// Destructor
CPoint2D::~CPoint2D() {
}
// Functions
bool CPoint2D::equals(CPoint2D cCPoint2D) {
bool aBool = this->equals(cCPoint2D.getX(), cCPoint2D.getY());
return aBool;
}
bool CPoint2D::equals(double cX, double cY) {
if (this->x != cX) {
return false;
}
if (this->y != cY) {
return false;
}
return true;
}
// Getter
double CPoint2D::getX() const {
return this->x;
}
double CPoint2D::getY() const {
return this->y;
}
// Setter
void CPoint2D::setX(double cX) {
this->x = cX;
}
void CPoint2D::setY(double cY) {
this->y = cY;
}