-3
クラス 'room'に 'push_back'ベクトル関数を使用する際に問題が発生しています。私は '部屋'クラスのプライベートベクトル変数に値をプッシュバックしようとしています。私は自分の問題は「push_back」機能の仕組みを誤解していると思っていますが、自分自身で問題を解決するために何が間違っているのか分かりません。クラスでpush_backを使用する際のトラブル
これは、問題領域である:
void room::assign_exit_index(int index) {
exit_index.push_back(index);
}
void room::assign_exit_direction(string direction) {
exit_direction.push_back(direction);
}
void room::assign_exit_locked(bool exit_locked) {
exit_locked.push_back(exit_locked);
}
void room::assign_gold(Money gold) {
gold.push_back(gold);
}
void room::assign_key(Key key) {
key.push_back(key);
}
void room::assign_potion(int potion) {
potion.push_back(potion);
}
void room::assign_have_weapon(bool have_weapon) {
have_weapon.push_back(have_weapon);
}
void room::assign_weapon_index(int weapon_index) {
weapon_index.push_back(weapon_index);
}
void room::assign_have_scroll(bool have_scroll) {
have_scroll.push_back(have_scroll);
}
void room::assign_scroll_index(int scroll_index) {
scroll_index.push_back(scroll_index);
}
void room::assign_have_monster(bool have_monster) {
have_monster.push_back(have_monster);
}
void room::assign_monster_index(int monster_index){
monster_index.push_back(monster_index);
}
これは '部屋' クラスの宣言です:
class Money{
public:
Money() {
value = 0;
}
Money(int amount) {
value = amount;
}
friend int operator +(Money& initial_amount, int money_added);
private:
int value;
};
この:これは 'お金' クラス宣言である
class room{
public:
room();
friend void generate_rooms (ifstream& input_file, vector<room>& rooms_f);
void assign_exit_index(int index);
void assign_exit_direction(string direction);
void assign_exit_locked(bool exit_locked);
void assign_gold(Money gold);
void assign_key(Key key);
void assign_potion(int potion);
void assign_have_weapon(bool have_weapon);
void assign_weapon_index(int weapon_index);
void assign_have_scroll(bool have_scroll);
void assign_scroll_index(int scroll_index);
void assign_have_monster(bool have_monster);
void assign_monster_index(int monster_index);
private:
int room_index;
vector<int> exit_index;
vector<string> exit_direction;
vector<bool> exit_locked;
vector<Money> gold;
vector<int> key;
vector<int> potion;
vector<bool> have_weapon;
vector<int> weapon_index;
vector<bool> have_scroll;
vector<int> scroll_index;
vector<bool> have_monster;
vector<int> monster_index;
};
コンパイラエラーです:
room.cc: In member function ‘void room::assign_exit_locked(bool)’:
room.cc:23:13: error: request for member ‘push_back’ in ‘exit_locked’, which
is of non-class type ‘bool’
exit_locked.push_back(exit_locked);
^
room.cc: In member function ‘void room::assign_gold(Money)’:
room.cc:29:6: error: ‘class Money’ has no member named ‘push_back’
gold.push_back(gold);
^
room.cc: In member function ‘void room::assign_key(Key)’:
room.cc:35:5: error: ‘class Key’ has no member named ‘push_back’
key.push_back(key);
^
room.cc: In member function ‘void room::assign_potion(int)’:
room.cc:41:8: error: request for member ‘push_back’ in ‘potion’, which is of
non-class type ‘int’
potion.push_back(potion);
^
room.cc: In member function ‘void room::assign_have_weapon(bool)’:
room.cc:47:13: error: request for member ‘push_back’ in ‘have_weapon’, which
is of non-class type ‘bool’
have_weapon.push_back(have_weapon);
^
room.cc: In member function ‘void room::assign_weapon_index(int)’:
room.cc:53:14: error: request for member ‘push_back’ in ‘weapon_index’,
which is of non-class type ‘int’
weapon_index.push_back(weapon_index);
^
room.cc: In member function ‘void room::assign_have_scroll(bool)’:
room.cc:59:13: error: request for member ‘push_back’ in ‘have_scroll’, which
is of non-class type ‘bool’
have_scroll.push_back(have_scroll);
^
room.cc: In member function ‘void room::assign_scroll_index(int)’:
room.cc:65:14: error: request for member ‘push_back’ in ‘scroll_index’,
which is of non-class type ‘int’
scroll_index.push_back(scroll_index);
^
room.cc: In member function ‘void room::assign_have_monster(bool)’:
room.cc:71:14: error: request for member ‘push_back’ in ‘have_monster’,
which is of non-class type ‘bool’
have_monster.push_back(have_monster);
^
room.cc: In member function ‘void room::assign_monster_index(int)’:
room.cc:77:15: error: request for member ‘push_back’ in ‘monster_index’,
which is of non-class type ‘int’
monster_index.push_back(monster_index);
^
クラスメンバはあなたのパラメータと同じものを使用しているので、パラメター。これをしなければならない場合は、 'this-> vector.push_back'を使用してください。 – Donnie
@Donnieコメントは回答できません。 – juanchopanza
@juanchopanza - そんなに些細な答えです - 私はドンニーが何をしたのか何か間違っているとは思わない。そして彼は正しい。 – Tim