[尝试构建时出错:
../../../../../../ Classes / MainApp.cpp:64:错误:未定义对'Enemies :: getHealth()'的引用
在MainApp.cpp中,我尝试创建float b,但是由于敌人-> getHealth()而导致问题。我把健康作为公共变量,然后就可以直接得到它。但是我也需要一个二传手,所以我必须解决这个getter问题。
MainApp.cpp:
enemy = new Enemies(this, 50); //Works
float a = enemy->health; //Works
float b = enemy->getHealth(); //Doesn't work, when I open that gives error.
MainApp.h:
#include "Enemies.h"
Enemies* enemy;
Enemies.h:
#ifndef __ENEMIES_H__
#define __ENEMIES_H__
#include "cocos2d.h"
class Enemies
{
private:
public:
Enemies(cocos2d::Scene *scene, float healthIn) {
};
float health;
int getHealth();
void setHealth(int healthIn);
void setPosition(float x, float y);
};
#endif // !__ENEMIES_H__
Enemies.cpp:
#include "Enemies.h"
USING_NS_CC;
Enemies::Enemies(cocos2d::Scene *scene, float healthIn) {
visibleSize = Director::getInstance()->getVisibleSize();
origin = Director::getInstance()->getVisibleOrigin();
enemySprite = Sprite::create("enemy.png");
enemySprite->setPosition(Point((visibleSize.width / 2) + origin.x, (visibleSize.height/2) + origin.y));
scene->addChild(enemySprite);
health = healthIn;
}
int Enemies::getHealth() {
return 42;
}
void Enemies::setHealth(int healthIn) {
health -= healthIn;
}
void Enemies::setPosition(float x, float y) {
enemySprite->setPosition(Point(x, y));
}
#include <iostream>
class Enemies
{
private:
public:
Enemies(float healthIn);
float health;
int getHealth();
void setHealth(int healthIn);
void setPosition(float x, float y);
};
Enemies::Enemies(float healthIn) {
health = healthIn;
}
int Enemies::getHealth() {
return 42;
}
void Enemies::setHealth(int healthIn) {
health -= healthIn;
}
void Enemies::setPosition(float x, float y) {
//enemySprite->setPosition(Point(x, y));
}
int main()
{
Enemies * enemy = new Enemies(50); //Works
float a = enemy->health; //Works
float b = enemy->getHealth(); //Doesn't work, when I open that gives error.
std::cout << b << std::endl;
}