+ 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

19th Nov 2019, 9:17 PM
Daniel Cooper
Daniel Cooper - avatar
11 Respuestas
+ 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 : )
20th Nov 2019, 8:26 AM
Ipang
+ 2
Ipang I figured it out. Basically, when I create an object, it should immediately be added to the vector automatically.
20th Nov 2019, 6:44 AM
Daniel Cooper
Daniel Cooper - avatar
+ 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
20th Nov 2019, 8:08 AM
Daniel Cooper
Daniel Cooper - avatar
+ 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.
20th Nov 2019, 8:41 AM
Daniel Cooper
Daniel Cooper - avatar
+ 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});
19th Nov 2019, 9:42 PM
rodwynnejones
rodwynnejones - avatar
+ 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
19th Nov 2019, 9:59 PM
Daniel Cooper
Daniel Cooper - avatar
19th Nov 2019, 11:38 PM
Dennis
Dennis - avatar
+ 1
Dennis Yes, but it wont let me access member variables. Why?
20th Nov 2019, 12:32 AM
Daniel Cooper
Daniel Cooper - avatar
20th Nov 2019, 12:49 AM
Daniel Cooper
Daniel Cooper - avatar
+ 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?
20th Nov 2019, 6:22 AM
Ipang
+ 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 👍
20th Nov 2019, 7:08 AM
Ipang