dimanche 6 octobre 2019

Unable to print a vector of pointers

We have some classes, class A that have a constructor that looks like this:

A::A(int num, bool boods, double diablo, std::vector<ClassB* > &c) {
    createobj();
    setNum(num);
    setboods(boods);
    setDiablo(diablo);
    c= this->c;  //Where c, is just a vector of pointer objects of class B
}

void A::createobj() {
    E e("e", 59, 0, 100);  //Where E is a derived class inherited from class B
    B *e = &e;
    c.push_back(e); 
}

//Then over at main:

main() {
    std::vector<ClassB* > c;
    A a(100, true, 1.21, c);

    std::cout << c.size();  //prints out 1 as expected...

    for(auto i : c){
        std::cout << i->getName() << std::endl; //instead of printing "e"
                                                //I get this from the console 
                                                //�
                                                //Segmentation Fault
    }
}

I have been working on this for over 12 hours, any help is greatly appreciated and I will dance at your wedding.

c vector is a private vector of pointers that was declared in class A's .h and only holds ClassB* objects.

Aucun commentaire:

Enregistrer un commentaire