Hello guys I am trying to overload [] i want to do this
Pokemon somePok;
somePok[new Pokemon{ "PIKACHU", 100 },
new Pokemon{ "CHARMANDER", 100 }];
and I have put this in class pokemon
Pokemon* &operator[] (Pokemon*);/*inside pokemon class*/
when pokemon class ends I have put this
Pokemon* &Pokemon::operator[](Pokemon*first)
{
return first;
}
but when i put something like this
somePok[new Trainer{ "ASH" },
new Pokemon{ "CHARMANDER", 100 }];
it passes compile and creates everything but when i do this
somePok[new Pokemon{ "PIKACHU", 100 },
new Trainer{ "ASH", 100 }];
it hits error in compiler
how can i fix this?
Though I can't even tell if that's what you're trying to do. No one knows what you're trying to do. We gave a friendly hint in the other thread that you're just abusing the language.
I'm flattered that you think I'm psychic, but I don't know what you're talking about.
Is a Trainer a Pokemon, or is a Pokemon a Trainer? Or are both Pokemon and Trainer subclasses of something?
The answer might be dynamic_cast, but generally that should be avoided, and you should use virtual calls instead. (Or perhaps avoid polymorphism and don't put Trainers in the same array as Pokemon)
Why do Pokemon and Trainer need to be part of the same array in the first place?
Why not just have an array of Pokemon?
And a Trainer owns ("has-a") an array of Pokemon.
This is not an array and I just dont want this to happen
somePok[new Trainer{ "ASH" },
new Pokemon{ "CHARMANDER", 100 }];
I just want to have only Pokemon object inside of []