包含向量的类:编写at方法

时间:2017-10-16 15:18:58

标签: c++ class vector at-command

#include <vector>
using namespace std;

class State {
private:
  vector<bool> probabilities;
public:
  State(int size) {
    probabilities = vector<bool>(size,false);
  };
  bool &at(int i) { return probabilities.at(i); };
};

我看到有关包含向量的类的几个问题。但是,我没有看到关于如何编写一个“at”方法的点,该方法采用包含向量的“at”。

上面的代码给出了:

state.cxx: In member function 'bool& State::at(int)':                                             
state.cxx:11:44: error: cannot bind non-const lvalue reference of type 'bool&' to an rvalue of type 'bool'                                                                                           
   bool &at(int i) { return probabilities.at(i); };    

所以我明显错过了有关引用如何工作的内容。

0 个答案:

没有答案