So I am writing a c++ OpenGl application that should test if two figures layed out with matches are identical. I have written an own class: the "figure" class. This class makes use of the two classes "Line" and "Point". Here you can see my implementation in the figure.h header:
#define SHINT short int
class Point
{
public:
std::pair<SHINT, SHINT> coord;
Point()
{
coord = std::pair<SHINT, SHINT>{ 0, 0 };
}
Point(int x, int y)
{
coord = std::pair<SHINT, SHINT>{ (short)x, (short)y };
};
};
class Line
{
public:
std::pair<Point, Point> pts;
Line()
{
pts = std::pair<Point, Point>{ Point(), Point() };
}
Line(Point p1, Point p2)
{
pts = std::pair<Point, Point>{ (Point)p1, (Point)p2 };
};
};
class Figure
{
public:
Figure() {};
private:
std::vector<Line> lines;
Point p1;
Point p2;
char* renderPath = FILEPATH;
public:
void DrawComponents();
void Clear();
void RemoveLine(int index);
void AddLine();
void SetWorking(int x, int y, bool segment); //true seg1, false seg2
void Render();
};
The implementation of the problematic functions are as follows:
void Figure::AddLine()
{
Line l1 = Line(p1, p2);
lines.push_back(l1);
}
void Figure::SetWorking(int x, int y, bool segment)
{
if (segment)
{
p1 = Point(x, y);
}
p2 = Point(x, y);
}
I have a fig*, that can be either set to the first figure or to the second Figure. You can do that with the help of an ImGui overlay. If you press your mouse button, a set of function is triggered:
selected->SetWorking(posX, posY, m_Select); //
if (m_Select)
selected->AddLine();
posX and posY are calculated relative to the mouse position and that is working properly. We than call the SetWorking() function on the figure pointer, which again calls a constructor in the Point and Sets the working Point to what posX and Y are. I have to Points that are used in turns. So if I click the first time, the first Point is set, if I press a second time, the second Point is set, and with two Points set, we push a new line into the vector. This alteration is achieved by the "segment" bool int the SetWorking() function. Points are saved as std::pairs<> that hold short ints, and Lines are saved as std::pairs<> that hold two points. But if we land there, an error occurs: "Exception thrown: write access violation. this was 0x14" The error is thrown in the utility file of the c++ STL: (line 292)
pair& operator=(_Identity_t<_Myself&&> _Right) noexcept(
conjunction_v<is_nothrow_move_assignable<_Ty1>, is_nothrow_move_assignable<_Ty2>>) /* strengthened */ {
first = _STD forward<_Ty1>(_Right.first); // <---- here
second = _STD forward<_Ty2>(_Right.second);
return *this;
}
And honestly, at this point I don't have the slightest clue as of what is going on there. My educated guess is, that something with the std::pair is going wrong, but I am to unskilled with c++ yet, so I don't really now how to solve these kinds of error