You defined drawRockets
as a global function instead of a member function of the Player
class.
undeclared identifier when function is in cpp file
-
26-06-2022 - |
Pregunta
I'm working on Space Invaders and in my Player Class I'm using a vector of a struct called point to store the coordinates of rockets. For some reason i'm getting "rocketVector : undeclared identifier" when I try to use it in the .cpp file.
Does anyone know why?
I'm still pretty new to C++ and I haven't been able to find a solution on google. it's starting to do my head in now :)
Thanks for any help!
#include <windows.h>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <stdio.h>
#include <MMSystem.h>
using namespace std;
class Player
{
public:
Player(void);
~Player(void);
void drawRockets(ISprite *r);
vector<point> rocketVector;
};
Player.cpp
void drawRockets(ISprite *r) {
// Draw appropriate number of rockets
for(int i = 0; i < rocketVector.size(); i++){
if( rocketVector[i].y < 0.0 ){
// Remove rockets off screen
rocketVector.erase(rocketVector.begin() + i);
}
else{
rocketVector[i].y -= 20;
r->draw(int(rocketVector[i].x), int(rocketVector[i].y));
}
}
}
Solución
Otros consejos
You need to specify that drawRockets
method is a member of the Player
class:
void Player::drawRockets(ISprite *r) {
// Draw appropriate number of rockets
for(int i = 0; i < rocketVector.size(); i++){
if( rocketVector[i].y < 0.0 ){
// Remove rockets off screen
rocketVector.erase(rocketVector.begin() + i);
}
else{
rocketVector[i].y -= 20;
r->draw(int(rocketVector[i].x), int(rocketVector[i].y));
}
}
}
You have some errors in your code :
First, when you define a method outside its class, you have to specify it is in a class-scope during the declaration like :
void Player::drawRockets( ISprite *r ) { ... }; // ^^^^^^^^
this will solve your
"rocketVector : undeclared identifier"
error.Here the operator of scope (
::
, two colons) is used to define a member of a class from outside the class definition itself.Also, it is a very bad practice to to
using namespace ...
in a header file, you should avoid that.If you remove the
using namespace ...
don't forget to transform :vector<point> rocketVector;
in
std::vector<point> rocketVector; // ^^^^^