user20259078
user20259078

Reputation: 1

Accessing specific objects using pointer to multiple arrays of pointers

EDIT: Pasting portion of code that is giving me this issue. Although in my effort to cleanup some of the comments before pasting it here I ended up with a different error. Still happens in the same place though: "munmap_chunk(): invalid pointer Aborted (core dumped)".

Learning C++; working on polymorphism. I created a virtual base class and the idea is to have multiple child classes, although I'm currently just stuck on one. My program cannot use vectors, and must include a lambda, so I thought one way to incorporate it would be to just have a function that loops through all my objects and sends the object to the lambda which simply displays the name.

My issue is that I'm trying to figure out how to write the function and lambda parameters/arguments so that it accepts and can work with all my objects. I thought using the base class to create an array of pointers would e the answer but it's resulting in a segmentation fault.

Perhaps I'm misunderstanding inheritance, or perhaps unclear about the way I should be using the pointers and looping through the arrays?

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

//Hokeemon Class parent to Child class(es)
class Hokeemon {
        friend ostream &operator<< (ostream &stream, Hokeemon &critter);
        public:
              //  int hat, eyes, mouth, health = 0, boredom = 0; string name;//bool alive;
           string name;
              //   virtual string changeName(string); // offers user to change critter name
             //   virtual void passTime(){health--; boredom++;};
             //   virtual void description(){};
                virtual string getName(){return name;};
};

class Playful : public Hokeemon{ //inherits from parent Hokeemon        
        public:
                Playful() {
                        cout << "Hokeemon's being hatched.\n";
                        name = "Test";         
                }   
                    
                ~Playful(){cout << "Hookemon deleted" << endl;};
};


void forEach(int objects, Hokeemon** ptr, int**, void(*func)(Hokeemon &critter));




int main() {
        int obj = 1, aliveSize = 3; int playSize;
        srand((time(NULL)));// seed for random numbers

        playSize = aliveSize;
        int* arraySizes[obj]{}; //to hold sizes of child class arrays
        Hokeemon* hPtr[obj];
        hPtr[0] = new Playful[playSize]; arraySizes[0] = &playSize; //creates pointer to array of pointers for objects of class Playful 

        forEach(obj, hPtr, arraySizes, [](Hokeemon &critter){ cout << critter.getName();}); //<< endl; critter.description();});


        delete hPtr[0];
        return 0;
}

void forEach(int obj, Hokeemon **hArray, int** array, void(*func)(Hokeemon &critter)){

        Hokeemon* temp = NULL; //create pointer to hold pointers in array

        for( int i = 0; i < obj; i++){ //this level loops through objects in array

                temp = hArray[i]; //assigns temp pointer to applicable class pointer
                for (int j = 0; j < *array[i]; j++){// loops through objects in class 
                         func(temp[j]);
                }
        }
}

I can't get it to print past the first object (ptr[0][0] if I'm understanding correctly). I've tried manually printing ptr[0][1] and I get a segmentation fault. How should I be doing it so that I can access the second object of the Child class? Again, I'd like to keep it general so that I can easily add in another Child class and just change the array slightly.

Upvotes: 0

Views: 47

Answers (0)

Related Questions