Reputation: 587
I just has a vector like that
vector<User*> users;
I know it is not a good programming style...
now I have a function
vector<User> getAllUser(void)
{
}
What I had tried is
vector<User> getAllUser(void)
{
vector<User> result;
for (vector<User*>::iterator it = users.begin(); it != users.end(); it++)
{
result.push_back(**it);
}
return result;
}
But it didn't work.
Could someone to help me? Thankyou very much. I am just a beginner to STL
Upvotes: 0
Views: 85
Reputation: 6145
If you must use pointers, use a nice std::shared_ptr<User>
wherever you would use User*
, and don't mix and match heap and stack allocated objects.
If you didn't really need to use pointers everywhere, make sure that User
has a copy constructor defind (eg, User::User(const User& rhs) { /* ... */ }
)
Upvotes: 1
Reputation: 55573
You code should work, but you made a typo:
result.push_bach(**it);
push_bach
is not a declared function for std::vector
, so I'm assuming that's where the error lies. I would recommend you get a decent compiler, which should point this error out to you right away, without having to go through stackoverflow.
To fix, use the proper method name, push_back
instead:
result.push_back(**it);
Upvotes: 3