Hello :) I am a very new programmer and cannot figure out why I am having this error. To explain, when I run the program (code below) with different values in the line
array2D *a = new array2D(320,240);
(for example, changing 320 and 240 to 32 and 24) the program crashes either sometime after executing the getSize function or after executing the prtValue function (more often the former). When I build the code, however, regardless of the values that I have in the above line, it returns 0 errors and 0 warnings.
I have tested the code on cpp.sh and that site accurately changes the values and outputs the correct/complete result every time, so I'm wondering if this is a CodeBlocks/my hardware issue? The debugger also only returns one issue, and it seems to be with the setValue function, but my untrained eye can't tell what's wrong.
Apologies for the ignorance. Again, I have almost no experience in this area and am sort of at a loss. Thank you in advance for any help that you might be able to provide.
#include <iostream>
using namespace std;
class array2D
{
protected:
int xRes;
int yRes;
float ** xtable;
public:
array2D (int xResolution, int yResolution);
void getSize(int &xResolution, int &yResolution);
void setValue(int x,int y,float val);
float getValue(int x,int y);
~array2D();
};
array2D::array2D(int xResolution, int yResolution)
{
xRes=xResolution;
yRes=yResolution;
xtable = new float*[xResolution];
for(int i=0;i < xResolution;i++)
{
xtable[i] = new float[yResolution];
}
for(int i=0;i < xRes;i++)
{
for(int j=0;j < yRes;j++)
{
xtable[i][j]=0;
}
}
}
void array2D::getSize(int &xResolution, int &yResolution)
{
xResolution=xRes;
yResolution=yRes;
cout << "Size of Array (rows, columns): " << xResolution << ", " << yResolution << endl;
}
void array2D::setValue(int x,int y,float val)
{
xtable[x][y] = val;
}
float array2D::getValue(int x,int y)
{
return xtable[x][y];
}
array2D::~array2D(){
cout << "Destructing array" << endl;
}
int main()
{
array2D *a = new array2D(32,24);
int xRes, yRes;
a->getSize(xRes,yRes);
for(int i=0;i < yRes;i++)
{
for(int j=0;j < xRes;j++)
{
a->setValue(i,j,100.0);
}
}
for(int j=0;j < xRes;j++)
{
for(int i=0;i < yRes;i++)
{
cout << a->getValue(i,j) << " ";
}
cout << endl;
}
a->~array2D();
}