Archived

This topic is now archived and is closed to further replies.

access violation error

This topic is 6367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why am I getting an access violation error with the following code?
class player
{
public:
    player(string, int, int, int, int, unsigned long, long);
    ~player(void);
    void buy_weapon(weapon *, int);
    void sell_weapon(weapon *);
    friend weapon;
private:
    string player_name;
    int weapon_status;
    int health;
    int armor;
    int pocket_money;
    unsigned long bank_money;
    long experience;
};

class weapon
{
public:
    weapon(int);
    ~weapon(void);
    int weapon_check(player *, int);
    friend player;
private:
    int c_array[9];
    int cost;
};

int weapon::weapon_check(player *ThePlayer, int wc)
{
if ((ThePlayer->weapon_status == 0) && (c_array[wc] <= ThePlayer->pocket_money))  // ERROR
    return 1;
else if (ThePlayer->weapon_status == 1)
    return 2;
else if (c_array[wc] > ThePlayer->pocket_money)
    return 3;
}

void menu_weapons(player *ThePlayer, weapon *TheWeapon)
{
char menu_choice;
char buf[2];
int weapon_select;
int done = 0;
int weapon_check;  // does user have enough money or a weapon already?

while(!done)
{
// list weapons

    cout << endl << "Your choice: ";

    menu_choice = getch();

    switch(toupper(menu_choice))
    {
        case ''1'':
        case ''2'':
        case ''3'':
        case ''4'':
        case ''5'':
        case ''6'':
        case ''7'':
        case ''8'':
        {
            weapon_check = TheWeapon->weapon_check(ThePlayer, weapon_select);
            if (weapon_check == 1)
            {
                buf[0] = menu_choice;
                buf[1] = ''\0'';
                weapon_select = atoi(buf);
                done = 1;
                weapon * TheWeapon = new weapon(weapon_select);
                ThePlayer->buy_weapon(TheWeapon, weapon_select);
                getch();
                menu_main(ThePlayer, TheWeapon);
                break;
            }
            else if (weapon_check == 2)
            {
                cout << endl << "You already have a weapon.";
                getch();
            }
            else if (weapon_check == 3)
            {
                cout << endl << "You don''t have enough money.";
                getch();
            }
        break;
        }
    }
}
 
The access violation error is occuring at the line indicated by "ERROR". Would anything have to do with comparing incompatible data types (signed, unsigned, etc.)? --- Joker2000 Stevie Ray Vaughan - The Legend

Share this post


Link to post
Share on other sites
most likely the access violation error has to do with referencing an invalid player object. make sure you are sending a valid player object to the method.

Share this post


Link to post
Share on other sites
I checked both scenarios and both appear to be within valid range. I'm trying to use C++Builder's debugger but I'm not very good at debugging.

However, I've set breakpoints and commented out some of the lines and the only ones that seem to cause the errors are the ones that compare c_array[wc] and ThePlayer->pocket_money.

---
Joker2000
Stevie Ray Vaughan - The Legend


Edited by - Joker2000 on July 11, 2000 12:16:21 AM

Share this post


Link to post
Share on other sites
i don''t use c++ builder. but in visual studio, you can step through the code and watch the values of variables. if you can do that in c++ builder then you might want to do that. also you can use an assertion to ensure that ((wc >= 0) && (wc <= 9)) and (player != NULL). other than that, everything looks fine. an access violation is almost always an attempt to dereference a invalid pointer.

Share this post


Link to post
Share on other sites
Hey Joker!

This d*mn board fooled me! hehe It said there were 0 replies to your message but it seems its all OK now.
But anyway as I read a bit of your code, lemme take a guess and you tell me if that was the error :

When you were checking for the weapon you were using :

    
switch (key)
{
case ''0'' :
case ''1'' :
...


And then calling the weapon_check function with the "key" as the wepaon value hoping it to be between 0 and 9, but actually it was from 48 to 57 (or is it 31-40?) cause thats the ASCII value of the 0 key to the 9 key, right? So you were accessing the 48th (or 31th) element of the array, not the 0th (0th?? eheh).

Was that the error?

Cya,
-RoTTer

Share this post


Link to post
Share on other sites