类对象未在范围中声明
Class objects not declared in scope
我目前正在遇到一个问题,我似乎无法围绕发生这种情况。在我的(Unsplit(程序中,我创建了一个定义实体对象的类,并且能够处理其创建和变量(正如我在添加
之前已经测试的那样(std::string getName(Entity)const;
std::string getType(Entity)const;
int getDamage(Entity)const;
int getHealth(Entity)const;
,但是当我这样做...即使它们已经在班上公开声明,我也能够完全呼叫initialize((;攻击((;和printStats((;很好,它看不到其他四个,因此无法被调用。
#include <iostream>
#include <string>
#include <math.h>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
class Entity
{
public:
Entity() { // default constructor
name = "Human";
type = "Normal";
damage = 1;
health = 100;
}
void printStats();
void Initialize(string, string, int, int); //transformer or setting function
void Attack(Entity&); //observer or getter function
std::string getName(Entity)const;
std::string getType(Entity)const;
int getDamage(Entity)const;
int getHealth(Entity)const;
private://data members and special function prototypes
std::string name;
std::string type;
int damage;
int health;
};
void summonEnemy(Entity&);
int main () {
/* initialize random seed: */
srand (time(NULL));
Entity Player;//declaring new class objects
Entity Enemy;//declaring new class objects
Player.Initialize("Player", "Normal", 10, 90);
summonEnemy(Enemy);
return 0;
}
void summonEnemy(Entity &target) {
target.Initialize("Enemy", "Normal", floor(rand() % 20 + 1), floor(rand() % 100));
cout << "An " << getType(target) << " type " << getName(target) << " has appeared with " <<
getHealth(target) << "HP and can do " << getDamage(target) << " damage.";
}
错误消息:
error:'getType' Was not defined in this scope.
error:'getName' Was not defined in this scope.
error:'getHealth' Was not defined in this scope.
error:'getDamage' Was not defined in this scope.
切断一些代码以将其缩小范围,以便仅可能出现问题的原因...但是说实话,这可能是我看不到的简单的东西。任何帮助。
您没有正确调用它们。他们是Entity
类的成员,而不是独立函数。从它们中删除Entity
参数,因为它们已经具有隐式Entity *this
参数,然后以这样的方式调用它们:
class Entity
{
public:
Entity(); // default constructor
...
std::string getName() const;
std::string getType() const;
int getDamage() const;
int getHealth() const;
...
};
Entity::Entity()
{
Initialize("Human", "Normal", 1, 100);
}
std::string Entity::getName() const
{
return name;
}
std::string Entity::getType() const
{
return type;
}
int getDamage() const
{
return damage;
}
int getHealth() const
{
return health;
}
void summonEnemy(Entity &target)
{
target.Initialize("Enemy", "Normal", floor(rand() % 20 + 1), floor(rand() % 100));
cout << "An " << target.getType() << " type " << target.getName() << " has appeared with " <<
target.getHealth() << "HP and can do " << target.getDamage() << " damage.";
}
getType
是Entity
的成员函数,因此您需要在Entity
对象上调用它:
target.getType();
在班上,您可以将其实现为:
class Entity {
...
std::string getType() const { return type; }
...
};
您的其他三个播放器也是如此。
相关文章:
- 错误:未在此范围内声明'reverse'
- 在基于范围的for循环中使用结构化绑定声明
- 错误:"imread"未在此范围内声明
- 我在范围内未声明的错误类有问题
- 未在此范围内声明错误 'xy'
- Socklen_t未在此范围内声明
- 如何从未知视图中声明范围::v3::View 变量
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- "Main"已在当前范围内声明
- c++ 变量在宏的扩展中没有在这个范围内声明
- 朋友声明的复杂范围界定规则有什么意义?
- C++ 在编译过程中 strtok 函数 Eclipse 说没有在范围内声明?
- 'num1'未在此范围内声明
- 在此范围内未声明错误"Clreol"
- 如何找到在本地范围内声明的变量的地址?
- 简化在 Pybind11 中为 C++ 模板类生成包装类:模板声明不能出现在块范围内
- C++模板专用化中的声明范围
- 这是有效的C++代码吗?(在声明范围之外使用本地类型)
- 函数声明范围
- 如何在类声明范围内使用“using space::function”