0

This is problem #2 from this previous question:

Inheritance in Arduino Code

Building off of Steven's answer, I do need the array that holds the pointers to persist outside of its scope, which is resulting in some weird behavior.

This is my "Board" class I have so far, that contains multiple child elements:

Board.h:

#ifndef Board_h
#define Board_h

#include <StandardCplusplus.h>
#include <serstream>
#include <string>
#include <vector>
#include <iterator>

#include "Arduino.h"
#include "Marble.h"
#include "Wall.h"

class Board
{
  public:
    Board();
    void draw(double* matrix);
  private:
    Marble marble;
    //std::vector<Actor> children;
    Actor* children[2];

};
#endif

Board.cpp:

#include "Arduino.h"
#include "Board.h"
#include <math.h>

#include <iterator>
#include <vector>

Board::Board()
{

}

void Board::create(double* _matrix, int _cols, int _rows) {

  Marble *marble = new Marble();
  Wall wall;
  children[0] = marble; 

  //children.push_back(marble);
  //children.push_back(wall);


}


void Board::draw(double* matrix) {
  Serial.println("board draw");
  children[0]->speak();  
}

In my "loop" function I am calling

board.draw(matrix);

which results in some nutty Serial code being written out.

Clearly I am not understanding the ins and outs of pointers in arrays in classes here.

2
  • in your loop function what is matrix?? Commented Feb 15, 2013 at 0:29
  • Is Marble a subclass of Actor? Is Marble::speak virtual? Is Actor::speak virtual? If the answer is yes to all three, your code should work. Commented Feb 15, 2013 at 0:31

1 Answer 1

1

You need to make Actor::speak virtual, the compiler uses dynamic binding for virtual methods.

class Actor
{
  public:
    Actor();
    virtual void speak();  // virtual
  private:
};
Sign up to request clarification or add additional context in comments.

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.