+ 1

Passing an array of objects as a function parameter

In the code below, I have trouble fixing the error I receive. I understand that, since I am passing an array of objects, the compiler wants a reference or a pointer as the actual function argument. My question here is how to solve that? Should I rebuild my array into one filled with pointers to the objects or is there another (more elegant) way? [And yes, I already searched the internet, but I couldnt find a satisfying answer.] https://code.sololearn.com/c6DWopnoBcQn/?ref=app

2nd Jan 2018, 12:49 AM
Shadow
Shadow - avatar
5 Respuestas
+ 3
Please consider providing a minimal example that shows your problem next time, it's tough to see through that much code! Anyway I think what's happening is that you in fact aren't passing an array. *it = CardLibrary::get()->getRandomCard(CardLibrary::classicLegendary[ClassicCards::LEGENDARY], ClassicCards::LEGENDARY); Here you only pass the `ClassicCards::LEGENDARY`-th card to getRandomCards (which happens to be out of bounds!), not the array. If you remove the subscript and just have `...->getRandomCard(CardLibrary::classicLegendary, ...`, the error disappears. Now your program still errors but I hope that gets you a step further! By the way, the "nice" way to deal with arrays is most of the time not to use arrays at all but rather std::vector. It has flexible size and is just all in all more convenient. But working with plain arrays is good for practice :)
2nd Jan 2018, 1:59 AM
Schindlabua
Schindlabua - avatar
+ 10
Not sure if this helps. Just keep in mind that arrays decay to pointers when passed to functions. You can choose to explicitly declare the function to accept a pointer as argument instead, it should also work fine. https://code.sololearn.com/cvSFcoFcIhya/?ref=app
2nd Jan 2018, 3:18 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
Bookmarking, will check back.
2nd Jan 2018, 2:29 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Thanks for your help, yeah, the code was messy and long, im sorry to force you to go through it. I indeed simply passed the array in the wrong way (ups...hehe...), however I still receive an error I absolutly dont get, so I'll have to code some classes from scratch I think. My bad. @Schindlabua, as to why I dont use vectors: Sure, vectors are more flexible with more options, but this comes with the price of additional memory space required. But I have a fixed amount of cards (copied from the actual game) that is never going to change (and shouldn't be allowed to), so I dont need the flexibility provided by vectors, since I wont delete or add any cards. So, I chose to go with arrays and save some memory space (might not be much, but hey...). Also, thanks for the sample code, Hatsy.
2nd Jan 2018, 5:38 PM
Shadow
Shadow - avatar
+ 1
Fair! But I'm one of these millennial programmers who is perfectly fine of dealing with the 10-20 byte overhead so I don't have to deal with the mental burden of having a weird number in my program that takes up a line of code (in your case the ClassicCards enum). Nothing wrong with arrays though in your case.
2nd Jan 2018, 6:04 PM
Schindlabua
Schindlabua - avatar