# Undefined reference to \`vtable for "abstract classname" even after derived class definition

**URL:** <https://forum.arduino.cc/t/undefined-reference-to-vtable-for-abstract-classname-even-after-derived-class-definition/1084221>\
**Category:** Programming\
**Created:** [January 30, 2023, 4:22pm UTC](https://forum.arduino.cc/t/undefined-reference-to-vtable-for-abstract-classname-even-after-derived-class-definition/1084221 "2023-01-30T16:22:38Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![zayedalzaabi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zayedalzaabi/32/914708_2.png) [@zayedalzaabi](https://forum.arduino.cc/u/zayedalzaabi)\
**Post date:** [January 30, 2023, 5:25pm UTC](https://forum.arduino.cc/t/undefined-reference-to-vtable-for-abstract-classname-even-after-derived-class-definition/1084221/6 "2023-01-30T17:25:53Z")

</div>

I've made the necessary changes to pass it in as a pointer.

Heres the gamehandler.h

```cpp
#ifndef GAMEHANDLER_H
#define GAMEHANDLER_H
#include <stdlib.h>
#include <Arduino.h>
#include "Target.h"
#include "Gamemode.h"
#include "SoloTimeMode.h"

class GameHandler {
private:
  bool _isPaused;
  bool _gameEnded;

  unsigned long _gameTimer;
  Gamemode* _gamemode;
  int _numTargets;
  Target* _targets;
  unsigned long _gameDuration;

public:
  GameHandler(Target targets[], int numTargets);
  void init();
  void selectGamemode(Gamemode &mode);
  void gameLoop();

  void handleTargetsLogic();
};

#endif

```

heres the gamehandler.cpp:

```cpp
#include "GameHandler.h"
#include <stdlib.h>
#include <Arduino.h>

GameHandler::GameHandler(Target targets[], int numTargets) {
  _targets = targets;
  _numTargets = numTargets;
  _isPaused = true;
  _gameEnded = false;
  // _gamemode = 0;

  _gameTimer = 0;
}

void GameHandler::init() {
  for (int i = 0; i < _numTargets; i++) {
    _targets[i].init();
  }
}

void GameHandler::selectGamemode(Gamemode &mode) {
  _gamemode = mode;
  _isPaused = false;
  _gameTimer = millis();
}

void GameHandler::gameLoop() {
  if (_isPaused) {
    return;
  }

  handleTargetsLogic();
}

void GameHandler::handleTargetsLogic() {
  for (int i = 0; i < _numTargets; i++) {
    _gamemode.prepareTargetHandler(_targets[i]);
    if (_targets[i].hasBeenShot()) {
      _gamemode.onTargetHitHandler(_targets[i]);
    }
  }
}

```

heres the gamemode.h file

```cpp
#ifndef GAMEMODE_H
#define GAMEMODE_H
#include <stdlib.h>
#include <Arduino.h>
#include "Target.h"
#include "TargetState.h"

// enum Gamemode { SOLO_TIME_CHALLENGE,
// SOLO_RANDOM_CHALLENGE,
// MULTI_RANDOM_CHALLENGE };

class Gamemode {
protected:
  unsigned long startTime;
  unsigned long endTime;

  int ledOneThreshold;
  int ledTwoThreshold;
  int ledAllThreshold;
  int ledNonThreshold;

public:
  virtual void handleGameModeLogic()=0;
  virtual void onTargetHitHandler(Target &target)=0;
  virtual void prepareTargetHandler(Target &target)=0;

};

#endif

```

solotimemode.h

```cpp
#ifndef SOLOTIMEMODE_H
#define SOLOTIMEMODE_H
#include <stdlib.h>
#include <Arduino.h>
#include "Gamemode.h"
#include "Target.h"

class SoloTimeMode : public Gamemode {
private:
  int _score;
  int _numTargets;

public:
  SoloTimeMode(int numTargets);
  void handleGameModeLogic() override;

  void prepareTargetHandler(Target &target) override;
  void onTargetHitHandler(Target &target) override;

  void endGame();
};

#endif

```

and the cpp

```cpp
#include <stdlib.h>
#include <Arduino.h>
#include "SoloTimeMode.h"

SoloTimeMode::SoloTimeMode(int numTargets) {
  _score = 0;
  startTime = millis();
  endTime = 0;
  _numTargets = numTargets;

  ledOneThreshold = 0;
  ledTwoThreshold = 0;
  ledAllThreshold = 0;
  ledNonThreshold = 0;
}

void SoloTimeMode::handleGameModeLogic() {
}

void SoloTimeMode::prepareTargetHandler(Target &target) {
  switch (target.getState()) {
    case LED_NON:
      target.setState(LED_ONE);
      break;
    case LED_TWO:
      break;
    case LED_ONE:
      break;
    case LED_ALL:
      break;
  }
}
void SoloTimeMode::onTargetHitHandler(Target &target) {
  switch (target.getState()) {
    case LED_NON:
      // do nothing
      break;
    case LED_TWO:
      break;
    case LED_ONE:
      // _score++;
      target.setState(LED_TWO);
      _score++;
      if (_score == _numTargets) { endGame(); }
      break;
    case LED_ALL:
      break;
  }
}

void SoloTimeMode::endGame() {
  endTime = millis();
}

```

Now this is the error im getting.

```cpp
C:\Users\LENOVO\Documents\Arduino\sketch_jan18a\GameHandler.cpp: In member function 'void GameHandler::selectGamemode(Gamemode&)':
C:\Users\LENOVO\Documents\Arduino\sketch_jan18a\GameHandler.cpp:25:15: error: cannot convert 'Gamemode' to 'Gamemode*' in assignment
   _gamemode = mode;
               ^~~~
C:\Users\LENOVO\Documents\Arduino\sketch_jan18a\GameHandler.cpp: In member function 'void GameHandler::handleTargetsLogic()':
C:\Users\LENOVO\Documents\Arduino\sketch_jan18a\GameHandler.cpp:40:15: error: request for member 'prepareTargetHandler' in '((GameHandler*)this)->GameHandler::_gamemode', which is of pointer type 'Gamemode*' (maybe you meant to use '->' ?)
     _gamemode.prepareTargetHandler(_targets[i]);
               ^ ~~~~~~~~~~~~~~~~~~~
C:\Users\LENOVO\Documents\Arduino\sketch_jan18a\GameHandler.cpp:42:17: error: request for member 'onTargetHitHandler' in '((GameHandler*)this)->GameHandler::_gamemode', which is of pointer type 'Gamemode*' (maybe you meant to use '->' ?)
       _gamemode.onTargetHitHandler(_targets[i]);
                 ^ ~~~~~~~~~~~~~~~~~

exit status 1

Compilation error: cannot convert 'Gamemode' to 'Gamemode*' in assignment

```

Heres the main arduino code:

```cpp
#include "Target.h"
#include "GameHandler.h"
#include "SoloTimeMode.h"

const int LED_PIN_1 = 7;
const int LED_PIN_2 = 6;
const uint8_t PIEZO_PIN_1 = A5;

const int LED_PIN_1a = 4;
const int LED_PIN_2a = 5;
const uint8_t PIEZO_PIN_2 = A0;

int counter = 0;

int targetSensitivity = 1000;

const int NUM_TARGETS = 2;
Target targets[NUM_TARGETS] = { Target(PIEZO_PIN_1, LED_PIN_1, LED_PIN_2),
                                Target(PIEZO_PIN_2, LED_PIN_1a, LED_PIN_2a) };

GameHandler gameHandler(targets, NUM_TARGETS);
SoloTimeMode mode(NUM_TARGETS);

void setup() {
  gameHandler.init();
  Serial.begin(9600);
  
  gameHandler.selectGamemode(mode);
}

void loop() {
  gameHandler.gameLoop();

  Serial.println(targets[0].getState());
}

```

I feel like im getting closer but still not there yet. I'm not sure what these errors mean.

I'll attach the files unzipped if that helps.  
[GameHandler.cpp](https://forum.arduino.cc/uploads/short-url/86rrL0KWBNGQJqwcMWdKevmVB2Y.cpp) (854 Bytes)  
[GameHandler.h](https://forum.arduino.cc/uploads/short-url/of0eMwgwWjenZhJSNRE3uc3vOTI.h) (551 Bytes)  
[Gamemode.h](https://forum.arduino.cc/uploads/short-url/xXOV3umkwyhzGcZcvzpyX52raFc.h) (624 Bytes)  
[sketch\_jan18a.ino](https://forum.arduino.cc/uploads/short-url/zsrsVXkb2pCshpRBxakt1RPe3DQ.ino) (739 Bytes)  
[SoloTimeMode.cpp](https://forum.arduino.cc/uploads/short-url/mFk0dUSL3L93NKoA9NdYatkemRL.cpp) (1.0 KB)  
[SoloTimeMode.h](https://forum.arduino.cc/uploads/short-url/zQsWiWwIAtymznncs8sYVWgkE5m.h) (455 Bytes)  
[Target.cpp](https://forum.arduino.cc/uploads/short-url/opQoI3XQWd3EFUBf6bVQs3sGVi0.cpp) (1.5 KB)  
[Target.h](https://forum.arduino.cc/uploads/short-url/ecyhq6M2Eul3CyC2z0tAn9UGgHh.h) (623 Bytes)  
[TargetState.h](https://forum.arduino.cc/uploads/short-url/k8VVjKRvxYLdFAbcrY0b0ujNvpc.h) (236 Bytes)

---

_[View the full topic](https://forum.arduino.cc/t/undefined-reference-to-vtable-for-abstract-classname-even-after-derived-class-definition/1084221)._
