【发布时间】:2018-10-13 02:50:15
【问题描述】:
我是 C++ 新手,所以我不确定我的方法是否正确,但我的问题是:
如何从另一个 .cpp 文件访问和更改在结构内部定义的变量,该结构位于映射内部?
我的 .h 文件的一部分:
struct Borough {
std::string name = "";
int num_players = 0;
Borough(std::string n) : name(n) {}
friend inline bool operator< (const Borough& lhs, const Borough& rhs){ return (lhs.name < rhs.name); }
friend inline bool operator==(const Borough& lhs, const Borough& rhs){ return (lhs.name == rhs.name); }
};
class Graph {
public:
typedef std::map<Borough, Vertex *> vmap;
vmap walk;
};
和(部分) player.cpp 文件:
#include <iostream>
#include <stack>
#include "player.h"
void Player::move() {
std::string current_loc = get_location();
std::cout << "\nWhere do you want to move to?" << std::endl;
display_branches(current_loc);
std::string selected_location;
std::getline(std::cin, selected_location);
// verification and placement of player:
if (verify_location(current_loc, selected_location)) {
set_location(selected_location);
// HERE IS WHERE I WANT TO MAKE Borough::num_players++;
std::cout << m_graph.walk.find(selected_location)->first.num_players << " <-- That's the number of players.\n";
}
}
我知道我可以显示这个数字,但是当玩家成功“移动”到那里时,我想通过增加 +1 来改变它。
【问题讨论】:
标签: c++ dictionary struct