0

I've a class PokemonCollection which has a private list which accepts a pair.

Now what I've to do is, when I make an object"collection" of that class PokemonCollection in main() function, I pass some pairs to the constructor, and in the constructor I've to initialize the private list of the class.

PokemonCollection collection({pair<string, size_t>("Pikachu", 25), 
pair<string, size_t> ("Raticate", 20), pair<string, size_t>("Raticate", 20), 
pair<string, size_t>("Bulbasaur", 1), pair<string, size_t>("Pikachu", 25), 
pair<string, size_t>("Diglett", 50)});

this confuses me alot, can someone kindly help me as I'm a beginner, also I've to print that private list too.

collection.print();

where print is public function of my class.

class definition:

class PokemonCollection{
    public: 
        void print();
    private: 
        list<pair<string, size_t>> pokemons_;
};

where "size_t" is an identifier for the pokemon name. I have this hint in my assignment for the constructor:

  /* constructor
   * initializes the collection to by copying the parameter
   */
PokemonCollection(const std::list<std::pair<std::string, size_t>>& pokemons){}

enter image description here

  • 4
    The member being a list doesn't change anything. If it was a plain `int`, how would you initialize it with a constructor parameter? Do the same thing for your list. – HolyBlackCat Oct 11 '22 at 16:47
  • sir I've edited and uploaded the hint for the constructor in my assignment, Its quite confusing. – Jack Sparrow Oct 11 '22 at 16:50
  • It is not clear what is confusing you - have you tried to add the constructor that you were given to your code? – Drew Dormann Oct 11 '22 at 16:51
  • I added ``` PokemonCollection(const std::list>& pokemons){ } but I don't know how to initialize the private member list of my class through that constructor. – Jack Sparrow Oct 11 '22 at 16:52
  • https://en.cppreference.com/w/cpp/language/constructor – Jarod42 Oct 11 '22 at 16:53
  • the list name is added, its pokemons_ apologies – Jack Sparrow Oct 11 '22 at 16:54
  • @JackSparrow *the deadline is near* -- Well, [read this concerning deadlines](https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest). – PaulMcKenzie Oct 11 '22 at 17:06
  • Despite what the hint says, it should be `PokemonCollection(std::list> pokemons) : pokemons_(std::move(pokemons)) {}` to avoid unnecessary copies. – HolyBlackCat Oct 11 '22 at 17:43

1 Answers1

1

how to initializes a collection (list) by copying the parameters from a constructor?

Just like you would initialize any other data member in the member initializer list:

class PokemonCollection{
    public: 
        void print();
        PokemonCollection(const std::list<std::pair<std::string,
//---------------------------------------------------VVVVVVVVVVVVVVV--->use member initializer list
                                size_t>>& pokemons): myList(pokemons)
                                {

                                }
    private: 
        list<pair<string, size_t>> myList;
};

Demo

Note that in your given example, you had not given any name to the list data member, so in my example I've named it myList. Then myList is initialized with the function parameter named pokemons using the member initializer list.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • I'm sorry, thats the problem, I don't understand how to do it. I'm new to list. – Jack Sparrow Oct 11 '22 at 16:56
  • @JackSparrow Ok, which exact part do you find confusing in my given example. Can you pinpiont. I would also recommend using a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). These books are also available as PDFs for free. See also this [working demo](https://godbolt.org/z/h5az15619). – Jason Oct 11 '22 at 16:57
  • what you did there :mylist(pokemons){ } in constructor confuses me, also the cpprefernce on internet is so professional wording and confusing. – Jack Sparrow Oct 11 '22 at 17:00
  • theres nothing in the body of constructor, how am I supposed to add all those pairs while calling from main() into my private list? or do this automatically copies everything into the private list? – Jack Sparrow Oct 11 '22 at 17:02
  • @JackSparrow `:myList(pokemons}` means that you're telling the compiler to **initialize** the data member named `myList` with the *initializer/argument* `pokemons`. You should refer to THE BEST book for beginners called **C++ Primer by Lippman**. These things are very well explained in that book. Also use the latest version of the book which is think is the 5th edition. Here is a link to the book [C++ Primer by Lippman 5th edition](https://zhjwpku.com/assets/pdf/books/C++.Primer.5th.Edition_2013.pdf). – Jason Oct 11 '22 at 17:03
  • tysm sir, as I've no time for further reading, can you help me in printing the list? – Jack Sparrow Oct 11 '22 at 17:05
  • *tysm sir, as I've no time for further reading* -- It is unreasonable to ask someone to drop everything their doing because you have no time. It doesn't work that way here. Next time, budget your time a little better. – PaulMcKenzie Oct 11 '22 at 17:09
  • @JackSparrow You're welcome. See Page number 265 of [this](https://zhjwpku.com/assets/pdf/books/C++.Primer.5th.Edition_2013.pdf) where you'll find a section with title *"Constructor Initializer List"*. The topic is explained in more detail there. – Jason Oct 11 '22 at 17:10
  • sure I'll read it all, I've posted an image above, can you kindly give me a hint for that? it asks for two constructors, the one you helped me with has worked. Also I'll try to do better next time thanks. – Jack Sparrow Oct 11 '22 at 17:12
  • @JackSparrow Do you want to print the list? If yes, then see this [working demo that prints the list](https://godbolt.org/z/fbqK1xEea). I've added some comment in that [demo](https://godbolt.org/z/fbqK1xEea) so that you can see what changes i've made. – Jason Oct 11 '22 at 17:17
  • 1
    tysm sir, I'm so thanksful. sure I'll see it. – Jack Sparrow Oct 11 '22 at 17:20
  • @JackSparrow You're welcome again :) Btw the demo uses *"range based for loop"* to print the list. – Jason Oct 11 '22 at 17:20
  • tysm sirrr..... – Jack Sparrow Oct 11 '22 at 18:06