In my class, I have multiple variables that are defined in a header, and are used elsewhere in the functions that use them. However, whenever I call a function or variable from within the function definition, I'm getting errors saying that it can't find the variable, even though there aren't any errors coming up at compile-time.
I've added a cout in the constructor for the class, which shows the address for all of these variables. Here is the error that is being shown:
Unhandled exception at 0x002442b6 in Fireworks.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.
Here are the variables and their locations:
X: 001B4F10
Y: 001B4F12
TARGET_X: 001B4F16
TARGET_Y: 001B4F18
And none of these memory locations match up to any of the locations pointed to by the compiler.
My class, the constructor, and the variables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class PARTSYS{
public:
PARTSYS( short sx, short sy, short targetx, short targety ){
x = sx;
y = sy;
target_x = targetx;
target_y = targety;
std::cout
<< "X: " << x
<< "\nAt address: " << &x
<< "\nY: " << y
<< "\nAt address: " << &y
<< "\nTARGET_X: " << target_x
<< "\nAt address" << &target_x
<< "\nTARGET_Y: " << target_y
<< "\nAt address " << &target_y
<< std::endl;
}
void process( void );
short int x, y, target_x, target_y;
};
|
The process( ) declaration was in there too, since that's the function that the error is being called from. This function is being called by a static function of a different class, which uses a pointer returned by another static function of that class to point to an object of this class (PARTSYS):
1 2 3 4 5 6 7 8 9 10
|
class MANAGER{
public:
static PARTSYS* get_slot( int index );
static void process( );
static PARTSYS *first_slot;
static int size;
};
|
And their definitions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
PARTSYS* MANAGER::get_slot( int index ){
if( index < 1 || index > size )
return 0;
PARTSYS* ret = first_slot;
for( int i=1; i<=index; i++ ){
ret = ret->child;
}
return ret;
}
void SYS::MANAGER::process( ){
for( int i=1; i<=size; i++ ){
get_slot( i )->process( );
}
}
|
And then the problem function is defined using:
1 2 3
|
void PARTSYS::process( ){
...
}
|
All variables and functions that belong to PARTSYS are being errored, so I don't really have to show what's in that function.
What am I doing wrong here?
P.S.- As a side note, I've also noticed that even though y is defined as type short int, it's taking up 4 bytes. Any reason as to why this might be?