+ 2
[Solved]Object Vector
Is it possible to push an object to a vector in that object's constructor? I can't figure it out, but I need an object to be automatically added to a vector when the object is created Solution: https://code.sololearn.com/cuwZM85B8FzP/?ref=app
11 Respostas
+ 3
Daniel Cooper
Good job đ
I would recommend to instantiate the 3 items in main, because `shop` may be executed multiple times.
Also it seems there's a problem with getting input in main, I got timeout warning. But perhaps it's just because your code requires interactive I/O to work conveniently : )
+ 2
Ipang I figured it out. Basically, when I create an object, it should immediately be added to the vector automatically.
+ 2
Ipang There we go. As for the reason, it's just to shorten the code a bit.
I needed to create an object and the object has to be stored in a vector to iterate through them.
Here's the code I'm using it for:
https://code.sololearn.com/cnDQeE61133m/?ref=app
+ 2
Ipang
Yea, works a whole lot better if you run it elsewhere.
But basically, the first input is if you went to continue
Input should be 1 here, otherwise it'll end the program. Next input is the items, it'll keep accepting input until input is 'f'. Then it restarts.
For example:
1
TV
Knife
f
0
It's written as request by another user, but I do believe there are several changes that could improve the program.
+ 1
Is this what you mean:-
http://www.cplusplus.com/reference/vector/vector/vector/
also...not shown in above link:-
std::vector<int> myvector ({1,2,3,4,5,6});
+ 1
rodwynnejones If you're asking if I mean the container, yes. I need to add an object to a vector when that object is created.
https://code.sololearn.com/cgn0U2kfMWHD/?ref=app
+ 1
You mean this?
https://code.sololearn.com/c0914TEHntqv/?ref=app
+ 1
Dennis Yes, but it wont let me access member variables. Why?
+ 1
What are you trying to do Daniel Cooper ?
As I understand it, until the constructor is completely done, the object may not be fully initialized.
What is the plan/need for this 'on create store' idea anyways? can you elaborate more on the thoughts?
+ 1
Okay Daniel Cooper
I'm glad you got it sorted out đ
Yes I understand your intention. It's the reason/purpose of the need for immediate object storing that I didn't clearly understand.
Anyways, would you mind sharing the workaround with the community here? might be useful for someone in the future.
Mark your question [SOLVED] if the problem had been solved also helps to emphasize that a solution has been found đ