'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
Solution 1:[1]
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
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RvdK |
