0

I got the problem with virtual methods of my C++ projects.

First of all i got the class graphics which presents:

#pragma once
class gameGraphics
{
public:
    gameGraphics();
    ~gameGraphics();
    virtual void paint();
};

and i got two classes, first one :

class brick 
    : public gameGraphics
{
protected:
    int R, G, B;
    bool ifDelete;
public:
    brick();
    ~brick();
    virtual void paint(int x, int y);
};

And the second one:

class superBrick :
    public brick, public gameGraphics
{
private:
    int bonusType;
public:
    superBrick();
    ~superBrick();
    void paint(int x, int y);
};

Then im trying to paint objects of this two classes, when my projects runs it shows error: "Unhandled exception at 0x74D3CB49 in project.exe: 0xC0000005: Access violation executing location 0x00000000.", while trying paint function for superBrick object.

for (int i = 0; i < WIDTH; i++)
{
    for (int k = 0; k < LENGTH; k++)
    {
        temp = &table[k][i];
        temp->paint(k, i);
    }
}

I dont know what is the reason of this error, i think i did the Polymorphism good.

Sorry for my english, thanks for reading and help!

Have a good night!

EDIT:

class of table here:

class gameTable : public gameGraphics
{
private:
    brick** table;
public:
    gameTable();
    ~gameTable();
    void paint(int CordX, int cordY);   
};
8
  • 2
    Your destructors need to be virtual. Commented Apr 27, 2014 at 21:01
  • 1
    I doubt the polymorphism would be the cause of your access violation, and you are not showing much code. You might want to virtualize the inheritance to gameGraphics from brick and superBrick, though. Read about the diamond problem. Commented Apr 27, 2014 at 21:03
  • I suspect that you have gone outside the bounds of table. Can you create this error with a single superBrick object? Commented Apr 27, 2014 at 21:04
  • 3
    An MCVE would help, particularly the declaration of table. I recommend reading How to Debug Small Programs to get a better idea of how to debug this. Commented Apr 27, 2014 at 21:07
  • 3
    (1) not virtual destructor, (2) multiple inheritance, (3) various signatures for virtual function, (4) ifDelete flag, (5) no use of const, (6) apparently using macros for constants, like WIDTH, (7) needless use of pointers (like temp), (8) use of raw arrays instead of e.g. std::vector, (9) double-star Commented Apr 27, 2014 at 21:11

2 Answers 2

1

I believe this is the source of your problem

    temp = &table[k][i];

You can't have polymorphism with an array of objects because they would all be of the same class.

You've omitted the definition of table but it probably should be an array of pointer that can reference different classes with the same parent class.

Sign up to request clarification or add additional context in comments.

Comments

0

Access violation executing location 0x00000000 . This means you are dereferencing a null ptr ( a pointer of address 0. So the access violation is that you can't reference memory at location 0. Either table is null or table[k] is null. Make sure table is initialized with brick ptrs.

Also check "Cheers and hth" comments for good coding suggestions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.