I am currently trying to create an application in C++ using a library called Simple2D.
https://github.com/simple2d/simple2d
There seems to be a problem with the line delete [] coor. I get a "has triggered a breakpoint" exception. When I delete the line everything works properly. I initialize the pointer with a new keyword so I assume I must use the delete. Why?
#include <simple2d.h>
#include <cmath>
#include <iostream>
class Function;
void render();
void update();
const int width = 800, height = 600;
class Point
{
float x, y;
};
class Function
{
friend class Window;
Point * points[];
void calculate_points()
{
}
public:
virtual float func(float x) = 0;
Function()
{
}
};
class Polynomial1
{
public:
float func(float x)
{
float y = pow(x, 3) + pow(x, 2) + 1;
return y;
}
};
class Axis
{
friend class Window;
float * coor;
int n_points;
float start, end, width;
char name;
void calculatePoints()
{
float scale = (end - start) / n_points;
for (int i = 0; i < n_points; i++)
{
*(coor + i) = start+(scale*i);
}
}
public:
Axis(char n='X', float st=-100, float e=100, int points = 10, int w = 3)
{
if (st < e)
{
name = n;
start = st;
end = e;
n_points = points;
width = w;
coor = new float[n_points];
calculatePoints();
}
else
{
std::cout << "Invalid data! Start cannot be of a larger value than end!";
}
}
~Axis()
{
if (coor != nullptr) {
delete[] coor;
}
}
};
void drawFunc(Function& func)
{
}
void drawAxis(Axis axis)
{
S2D_DrawLine(0, 300, 800, 300,
3,
0, 0, 0, 1,
0, 0, 0, 1,
0, 0, 0, 1,
0, 0, 0, 1);
}
void render()
{
}
void update()
{
Axis ax1;
drawAxis(ax1);
}
int main(int argc, char* argv[])
{
S2D_Window* window = S2D_CreateWindow(
"Graphing Calculator", // title of the window
width, height, // width and height
render, update, // callback function pointers (these can be NULL)
S2D_RESIZABLE // flags
);
window->viewport.mode = S2D_STRETCH;
window->background.r = 1.0;
window->background.g = 1.0;
window->background.b = 1.0;
S2D_Show(window);
return 0;
}