麻烦在Class中使用push_back

时间:2017-04-17 19:07:48

标签: c++

我在使用' push_back'时遇到问题我班级的矢量功能' room'。我试图将值推回到“房间”中的私有向量变量。类。我认为我的问题只是误解了如何推动'功能有效,但我不知道自己做错了什么,以便自己纠正问题。

这是问题所在:

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 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;

};

这是&#39; money&#39;类声明:

class Money{

public:

 Money() {
     value = 0;
 }

 Money(int amount) {
     value = amount;
 }

 friend int operator +(Money& initial_amount, int money_added);

private:

 int value;

};

这是编译器错误:

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);
               ^

1 个答案:

答案 0 :(得分:1)

您正在呼叫push_back上的参数,而不是您的班级成员。使用this->引用您的成员或使参数和成员的名称不同(即使用下划线为您的成员变量添加前缀)