I’m trying to learn C++ currently, but I’m having issues with the code below.
class Vector2
{
public:
double X;
double Y;
Vector2(double X, double Y)
{
this->X = X;
this->Y = Y;
};
SDL_Rect * getSdlOffset()
{
SDL_Rect * offset = new SDL_Rect();
offset->x = this->X;
offset->y = this->Y;
return offset;
};
};
Visual studio throws throw the following error when calling getSdlOffset()
An unhandled exception of type
‘System.AccessViolationException’
occurred in crossEchoTest.exeAdditional information: Attempted to
read or write protected memory. This
is often an indication that other
memory is corrupt.
I’ve got a C#/java background, and I’m lost… Any help would be much appreciated.
You never initialized X or Y… what do you those values might be? More than likely they are point to 00000X00(I am rusty this may not be the right address, but you are pointed to memory outside of your programs allocated space… thus the “GPF” I was C/C++ “convert” to Java(over 11 years ago) so I can appreciate your ideas of how a pointer might behave–I can assure you that pointers are the most difficult part of C/C++ to understand, so you are on the right track in your learning. Just keep in mind that unlike Java/C#, C/C++ do not keep you from hurting yourself or the OS memory space/memory space of other programs. I alway remember what a teacher once told me when I was learning C–“With C you get a Kevlar boot and a gun, it is up to you whether or not you put the boot on before you shoot yourself in the foot, because you will shoot yourself at some point…” Good luck to you on learning C++, just hang in there and don’t get discouraged.
WM