I’m very new to C++ and I have a problem passing a vector of pointers to objects. Specifically the problem is the objects are a subclass of the class the method is in. I’m getting an error:
'Player' was not declared in this scope
Player is a subclass of Visual and in my file Visual.cpp I have the following function which is also declared in my header file.
bool Visual::DrawStatusInformation(Timer* timer, std::vector<Player*>* playerList) {
// Draw Info
}
I have tried forward declaring class Player; in my header file but I get the error:
error: invalid use of incomplete type ‘struct Player’
error: forward declaration of ‘struct Player’
I think the problem is that my definitions are circular, Player extends Visual but Visual must know about Player in order to be able to deal with a vector of Player pointers.
My question is:
If it is possible, how can I use a subclass in a parameter of a superclass method?
By files:
Visual.h& move implementations to implementation filePlayerPlayer.hThis just fixes the compilation error, but your design also seems faulty. If
Playeris derived fromVisual, why does the methodexist? I’m sure you can replace it with
and override it in
Player. Also, why avector*and not avector&?