我正在使用太空入侵者和我的玩家类I我使用一个名为point的结构向量来存储火箭的坐标。由于某些原因,我得到了" rocketVector:未声明的标识符"当我尝试在.cpp文件中使用它时。
有谁知道为什么?
我对C ++还很陌生,但我还没能在谷歌上找到解决方案。它现在开始做我的头了:))
感谢您的帮助!
#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));
}
}
}
答案 0 :(得分:4)
您将drawRockets
定义为全局函数,而不是Player
类的成员函数。
答案 1 :(得分:3)
您需要指定drawRockets
方法是Player
类的成员:
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));
}
}
}
答案 2 :(得分:0)
您的代码中存在一些错误:
首先,当您在类之外定义一个方法时,您必须在声明期间指定它在类范围内,如:
void Player::drawRockets( ISprite *r ) { ... };
// ^^^^^^^^
这将解决您的"rocketVector : undeclared identifier"
错误。
这里范围(::
,两个冒号)的运算符用于从类定义本身之外定义类的成员。
此外,在头文件中using namespace ...
是一种非常糟糕的做法,你应该避免这种做法。
如果删除using namespace ...
,请不要忘记转换:
vector<point> rocketVector;
在
std::vector<point> rocketVector;
// ^^^^^