Problem with calling methods from object cast as its superclass

I am a C# programming trying to come to grasps with the difference in c++ to program a 3 tier behavior state machine for an Arduino robot I am working on.

The problem I am having is when calling a method on a derived object when the object is casted as a pointer to its superclass. Instead of calling the method it is just dumping out random memory.

I’ve been all over the map trying to figure this out, from declaring some of the methods as pure virtuals in the base abstract class to global vs dynamic declaration of the objects.

I have gotten it to work on the top tier by instantiating the object dynamically, but on the next tier down I’m running into the same problem, even though I have it implemented in the same way.

Here is my object schema:

Here are the abstract base classes:

Motivator – responsible for picking active behaviors
.h Motivator.h - Pastebin.com
.cpp Motivator.cpp - Pastebin.com

Behavior – responsible for coordinating tasks
.h Behavior.h - Pastebin.com
.cpp Behavior.cpp - Pastebin.com

Task – actually does something functional
.h Task.h - Pastebin.com
.cpp Task.cpp - Pastebin.com

Here are the derived classes which extend the above abstract classes:

WandererMotivator – extends Motivator
.h WandererMotivator.h - Pastebin.com
.cpp WandererMotivator.cpp - Pastebin.com

BWander – extends Behavior
.h BWander.h - Pastebin.com
.cpp BWander.cpp - Pastebin.com

Stop – extends task
.h Stop.h - Pastebin.com
.cpp Stop.cpp - Pastebin.com

Forward – extends task
.h Forward.h - Pastebin.com
.cpp Forward.cpp - Pastebin.com

Here is the top level program: Wanderer1.ino - Pastebin.com

The example I have working is on the Motivator level. The base Motivator class has a method called executeBehaviors() that looks for active behaviors and calls their execute() method. You can see where this happens on line 28 of Motivator.cpp. There is an array of behaviors declared in Motivator.h on line 22. It is populated with the method Motivator.addBehavior(Behavior* b) which can be found in Motivator.cpp on line 37.

To get this example to work I had to use the syntax in WandererMotivator.cpp on line 6: addBehavior(new BWander()); When declaring it like this:
BWander wander;

Then calling addbehavior(&wander) would result in random memory being dumped to the serial window. Changing this to using the dynamic new keyword has let this part function like it should.
Okay, so the part I’m still struggling with is in BWander.cpp line 21 is where the method starts. The method calls are on line 33 and 38. This should be calling the execute methods on the instances of Stop and Forward tasks that are instantiated in the BWander.init() method.

I have been screwing around with this for days now, I’ve tried everything I can think of. Please someone point out what I am doing wrong here.

Task* tasks[];

Maybe you need to declare how many elements the array of pointers must be able to hold?

static const byte maxTasks = 20;
Task* tasks[maxTasks];

And then check the limit when you add a task.

void Behavior::addTask(Task* t)
{
    if (taskCount < maxTasks)
    {
        tasks[taskCount] = t;
        taskCount++;
    }
    else
    {
        // handle exception
    }
}

How about zipping up your entire project and attaching that to a post? Trying to reconstruct it from a dozen links from Pastebin is not an enticing prospect.

How to use this forum

I'll post the whole project later this evening. This was my first post to the forum and I was unaware I could attach a file, that would have been FAR easier than posting to pastebin.