RiseHit
RiseHit

Reputation: 319

how to get vector from another class in c++

I am trying to pass a vector plan from a class Administrator, to a class User to use the vector in the report method void report() of this last class, but it seems that the vector arrives empty.

I will shorten the code to leave you a structure that can be better understood

file Administrador.h

class Administrador : public User
{
    public:
        vector<string> VectorAsign();
};

file Administrador.cpp

vector<string> Administrador::VectorAsign()
{
    vector<string> plan = {"one", "two"};
    return plan;
}

file User.h

class User
{
    public:
        vector<string> plan;
        void Report(vector<string> plan);
};

file User.cpp

void User::Report(vector<string> plan)
{
    this->plan= plan;
    for (int i = 0; i < plan.size(); i++)
    {
        cout << "Date of User::Report" << plan[i] << endl;
    }
}

file main.cpp

int main(){
    vector<string> plan;

    Administrador Admin;
    Admin.VectorAsign();

    User user;
    user.Report(plan);

    return 0
}

I've tried a lot, but I can't, is there a better way to pass this vector to another class? thank you

Upvotes: 0

Views: 53

Answers (1)

RvdK
RvdK

Reputation: 19800

VectorAsign returns a vector but you are not storing it into a variable.

int main(){
    
    Administrador Admin;
    vector<string> plan = Admin.VectorAsign();

    User user;
    user.Report(plan);

    return 0
}

Upvotes: 2

Related Questions