如何创建一个存储类的所有实例的向量?那么我如何迭代它们并调用它们的成员函数之一呢?
这是我一直在尝试做的一个简明示例。
#include <vector>
struct Entity{
Entity::Draw(){
// do drawing things
}
};
static std::vector<Entity> entities;
Entity player;
Entity enemy;
void renderEntities() {
for (std::vector<Entity>::iterator iter = entities.begin();
iter < entities.end();
iter++) {
iter->Draw; // Error in the example. I'm using Draw(); in the actual code.
}
但是 renderEntities() 没有做任何事情。如果我使用例如 player->Draw,则 Draw 成员函数有效。我要么搞砸了向量或迭代器,要么搞砸了两者,我不知道如何修复它。我尝试过使用引用和指针,我认为这是要做的事情,但是每当我尝试使用时,我都会遇到无法修复的错误。
更新:感谢所有帮助,我学到了很多东西。但是我的 render_entities 函数仍然没有做任何事情。这是所有的代码。
任何以 terminal_ 开头的函数调用都来自 BearLibTerminal 库。
主文件
#include <BLT/BearLibTerminal.h>
#include <iostream>
#include <string.h>
#include <vector>
#include "entity.h"
const int WindowSizeX{50};
const int WindowSizeY{20};
const std::string Title{"BLT Test"};
const std::string Font{"../res/SourceCodePro-Regular.ttf"};
const int FontSize{24};
bool quit_game{false};
static Entity player;
static Entity enemy;
void initialize();
void handle_input(int key, Entity &entity);
void draw_player(int x, int y, const char *symbol);
void render_entities();
void clear_entities();
int main() {
initialize();
while (!quit_game) {
terminal_refresh();
int key{terminal_read()};
if (key != TK_CLOSE) {
handle_input(key, player);
}
else {
quit_game = true;
break;
}
clear_entities();
}
terminal_close();
return 0;
}
void initialize() {
terminal_open();
std::string size{"size=" + std::to_string(WindowSizeX) + "x" +
std::to_string(WindowSizeY)};
std::string title{"title='" + Title + "'"};
std::string window{"window: " + size + "," + title};
std::string fontSize{"size=" + std::to_string(FontSize)};
std::string font{"font: " + Font + ", " + fontSize};
std::string concatWndFnt{window + "; " + font};
const char *setWndFnt{concatWndFnt.c_str()};
terminal_set(setWndFnt);
terminal_clear();
player.x = 0;
player.y = 0;
player.layer = 0;
player.symbol = "P";
player.color = "green";
enemy.x = 10;
enemy.y = 10;
enemy.layer = 0;
enemy.symbol = "N";
enemy.color = "red";
}
void handle_input(int key, Entity &entity) {
int dx{0};
int dy{0};
switch (key) {
case TK_LEFT:
case TK_H:
dx = -1;
dy = 0;
break;
case TK_RIGHT:
case TK_L:
dx = 1;
dy = 0;
break;
case TK_UP:
case TK_K:
dx = 0;
dy = -1;
break;
case TK_DOWN:
case TK_J:
dy = 1;
dx = 0;
break;
case TK_Y:
dx = -1;
dy = -1;
break;
case TK_U:
dx = 1;
dy = -1;
break;
case TK_B:
dx = -1;
dy = 1;
break;
case TK_N:
dx = 1;
dy = 1;
break;
case TK_ESCAPE:
quit_game = true;
break;
}
player.Move(dx, dy);
if (player.x > WindowSizeX - 1) {
player.x = WindowSizeX - 1;
}
else if (player.x < 0) {
player.x = 0;
}
if (player.y > WindowSizeY - 1) {
player.y = WindowSizeY - 1;
}
else if (player.y < 0) {
player.y = 0;
}
player.Draw(); // This works.
enemy.Draw(); // So do this.
entity.Draw(); // This draws only player.
render_entities(); // This doesn't do anything.
// Player X and Y are printed out correctly, Entities is always 0.
std::cout << "Player X: " << player.x << std::endl;
std::cout << "Player Y: " << player.y << std::endl;
std::cout << "Entities: " << entities.size() << std::endl;
}
void render_entities() {
for (auto entity : entities) {
entity->Draw();
}
}
void clear_entities() {
for (auto entity : entities) {
entity->Clear();
}
}
实体.h
#ifndef ENTITY_H_
#define ENTITY_H_
struct Entity {
int x;
int y;
int layer;
const char *symbol;
const char *color;
Entity();
~Entity();
void Move(int dx, int dy);
void Draw();
void Clear();
};
static std::vector<Entity *> entities;
#endif /* ENTITY_H_ */
实体.cpp
#include <BLT/BearLibTerminal.h>
#include <vector>
#include <algorithm>
#include "entity.h"
Entity::Entity() {
entities.push_back(this);
}
// Entity(const Entity &) : Entity() {}
// I get an "expected unqualified-id" when I uncomment this. Why?
Entity::~Entity() {
auto iter = std::find(entities.begin(), entities.end(), this);
if (iter != entities.end())
entities.erase(iter);
}
void Entity::Move(int dx, int dy) {
this->x += dx;
this->y += dy;
}
void Entity::Draw() {
terminal_layer(this->layer);
terminal_color(color_from_name(this->color));
terminal_print(this->x, this->y, this->symbol);
}
void Entity::Clear() {
terminal_layer(this->layer);
terminal_print(this->x, this->y, " ");
}
在 main.cpp 中,在 handle_input() 的底部,您将看到...
player.Draw(); // This works.
enemy.Draw(); // So do this.
entity.Draw(); // This draws only player.
render_entities(); // This doesn't do anything.
// Player X and Y are printed out correctly, Entities is always 0.
std::cout << "Player X: " << player.x << std::endl;
std::cout << "Player Y: " << player.y << std::endl;
std::cout << "Entities: " << entities.size() << std::endl;