0
#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); };
};

I see a couple of questions about classes that contain a vector. However, I do not see the point addressed of how to write an "at" method that takes the "at" of the included vector.

The above code gives:

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

So I'm clearly missing something about how references work.

Victor Eijkhout
  • 5,088
  • 2
  • 22
  • 23

0 Answers0