Georgian Gherasim
Georgian Gherasim

Reputation: 123

how to change the visibility of a vector of unique pointers

In the ONG class i created an add function that adds a participant (director, administrator, staff) in a vector of participants.

std::vector<unique_ptr<Participant>> ls;

I tried to put the vector as the public variable out of function but it didn't work

When I want to add inside the function everything works correctly, but when I put the list out of function it gives me an error

class ONG : public Participant {
private:
    
public:
    std::vector<unique_ptr<Participant>> ls;
    ONG() = default;
    void add(unique_ptr<Participant> part) {

        part->tipareste();
 
        ls.emplace_back(std::move(part));

        for (const auto& i : ls) {
            i->tipareste(); 
        }
        
    }
};

here is the whole code:

#include <iostream>
#include <assert.h>
#include <vector>
#include <memory>
#include <variant>
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>

using namespace std;

struct AtExit
{
    ~AtExit() { _CrtDumpMemoryLeaks(); }
} doAtExit;


class Participant {

public:
    virtual void tipareste() = 0;
    bool eVoluntar = true;
    virtual ~Participant() = default;
};
class Personal : public Participant {
private:
    string nume;
public:
    Personal() = default;
    Personal(string n) :nume{ n } {}
    void tipareste() override {
        cout << nume;
    }
};

class Administrator : public Personal {
public:
    std::unique_ptr<Personal> pers;
    Administrator() = default;
    Administrator(Personal* p) : pers{ p } {}
    void tipareste() override {
        cout << "administrator ";
        pers->tipareste();
    }
};

class Director : public Personal {
public:
    std::unique_ptr<Personal> pers;
    Director() = default;
    Director(Personal* p) : pers{ p } {}
    void tipareste() override {
        cout << "director ";
        pers->tipareste();
    }
};

class Angajat :public Participant {
public:
    std::unique_ptr<Participant> participant;
    Angajat() = default;
    Angajat(Participant* p) : participant{ p } { this->eVoluntar = false; /*p->eVoluntar = false;*/ }
    void tipareste() override {
        cout << "anjajat ";
        participant->tipareste();
    }
};

class ONG : public Participant {
private:
    
public:

    ONG() = default;
    std::vector<unique_ptr<Participant>> ls;    
   void add(unique_ptr<Participant> part) {
 
        ls.emplace_back(std::move(part));
        
    }
};


int main() {


    ONG* ong{};

    std::unique_ptr<Participant> aba = std::unique_ptr<Personal>(new Personal("Will"));
    ong->add(std::move(aba));
    
   
}

Upvotes: 3

Views: 82

Answers (1)

user
user

Reputation: 944

The problem is that class ONG is an abstract class because it inherits from Participant which has a pure virtual function.

If you define

    void tipareste() {
 //Do stuff
    }

inside ONG (or inside a class which ONG inherits) then assign an ONG object to the ONG pointer

int main() {

    std::shared_ptr<ONG> ong = std::make_shared<ONG> ();

    std::unique_ptr<Participant> aba = std::unique_ptr<Personal>(new Personal("Will"));
    ong->add(std::move(aba));

}

can be ran

Upvotes: 3

Related Questions