2D Array of objects with virtual child methods -> "vtable referenced from" error

71 views Asked by At

In my header for the Tile class, I defined a pure virtual method with no definition in the implementation: virtual void setVals(int ID) = 0;

The two classes which inherit Tile (Terrain and Actor) both overwrite the setVals method with the implementations:

void Terrain::setVals(int ID){
    switch(ID){
        case 1 : GFX_ = '.'; name_ = "Grass"; desc_ = "Some grass"; break;
        default: GFX_ = '?'; name_ = "Error"; desc_ = "Error"; Tile::isPassable_ = false; break;
    }
}

and

void Tile::setVals(int ID){
    switch(ID){
        case 1 : GFX_ = '?'; name_ = "Nothing"; desc_ = "You shouldn't be seeing this"; break;
        case 0 : GFX_ = '@'; name_ = "Player"; desc_ = "The Player"; break;
        default: GFX_ = '?'; name_ = "Error"; desc_ = "Error"; Tile::isPassable_ = false; break;
    }
}

respectively. An 2D array of each of these child classes is initialized in the Map class:

Terrain terrain_[HEIGHT][WIDTH];
Actor actors_[HEIGHT][WIDTH];

(where HEIGHT and WIDTH are constant ints). But when the program runs, the program returns a runtime error reading "'vtable for Actor', referenced from:". Am I making a mistake in how I'm initializing these methods or objects?

1

There are 1 answers

2
Cory Kramer On BEST ANSWER

You said that your base class is Tile, and has the following pure virtual method

virtual void setVals(int ID) = 0;

But yet you went on to define it?

void Tile::setVals(int ID){
    switch(ID){
        case 1 : GFX_ = '?'; name_ = "Nothing"; desc_ = "You shouldn't be seeing this"; break;
        case 0 : GFX_ = '@'; name_ = "Player"; desc_ = "The Player"; break;
        default: GFX_ = '?'; name_ = "Error"; desc_ = "Error"; Tile::isPassable_ = false; break;
    }
}

You need to implement Actor::setVals if that is a derived class from Tile