binding reference of type 'unsigned long' to value of type 'const unsigned long' drops 'const' qualifier

I defined two functions for my Solver class as follows:

solver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    class Solver: public DSPOMDP {
         friend class State;
    protected:
            std::vector<State*> states_;
            std::vector<OBS_TYPE> obs_;
       
    private:
          unsigned long histObs = 0;
          const unsigned BitsPerValue = 3;
          const unsigned Mask = (1u << BitsPerValue) - 1;
    
    public:
          Solver();
          static Solver* current_;
          void store(unsigned long &n, unsigned x) const;
          unsigned get_max(unsigned long n) const;
          double ObsProb(OBS_TYPE obs, const State& state, ACT_TYPE action) const;
    };

solver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    using namespace std;
    
    Solver* Solver::current_ = NULL;
    State::State() {
    }
    
    State::State(int _state_id) {
    	state_id = _state_id;
    }
    
    State::~State() {
    }
    
    Solver::Solver() {
    	current_ = this;
    }
    
    void Solver::store(unsigned long &n, unsigned x) const{
    
        n = (n << BitsPerValue) | x;
    }
    
    unsigned Solver::get_max(unsigned long n) const{
    
        unsigned m = 0;
        for ( ; n; n >>= BitsPerValue) 
            if ((n & Mask) > m) 
               m = n & Mask;
        return m;
    }
    double Solver::ObsProb(OBS_TYPE obs, const State& state,
        ACT_TYPE action) const {
          store(histObs,obs_[state.state_id]);
          return obs == get_max(histObs);
    }

I am getting these errors:
1
2
3
4
5
6
7
8
9
10
11
    src/solver.cpp:334:19: error: binding reference of type 'unsigned long' to value of type 'const unsigned long' drops 'const' qualifier
                store(histObs,obs_[state.state_id]);
                      ^~~~~~~
    src/solver.cpp:48:44: note: passing argument to parameter 'n' here
    void Solver::store(unsigned long &n, unsigned x) const{
                                               ^
    src/solver.cpp:358:13: error: binding reference of type 'unsigned long' to value of type 'const unsigned long' drops 'const' qualifier
          store(histObs,obs_[state.state_id]);
                ^~~~~~~
    src/solver.cpp:48:44: note: passing argument to parameter 'n' here
    void Solver::store(unsigned long &n, unsigned x) const{
^
I am trying to define the variable histObs inside the class which can be used in different methods of the class and keep track of all the changes made in the obs_vector. I don't know how it should be implemented in a right and efficient way?!! How can I fix these errors and correctly set up histObs variable?
Last edited on
Solver::ObsProb() is a const function, but Solver::store() changes the value of its first parameter. If you need to call store() from there then ObsProb() should be non-const.
@helios but I can not change the definition of ObsProb() because it should be used with other classes that I didn't write in the DSPOMDP class. Is there anyway I can fix my problem?
Also, store and get_max should be static functions (not const members) since they don't need access to the object (they could be separate functions). Just remove the const in both the declarations and definitions and put static before their declarations.
Last edited on
@dutch So following your suggestion, I should do
1
2
3
4
5
public:
      Solver();
      static Solver* current_;
      static void store(unsigned long &n, unsigned x);
      static unsigned get_max(unsigned long n);

in the header and define them like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
void Solver::store(unsigned long &n, unsigned x) {

    n = (n << BitsPerValue) | x;
}

unsigned Solver::get_max(unsigned long n){
    values from 0 to 7.
    unsigned m = 0;
    for ( ; n; n >>= BitsPerValue) 
        if ((n & Mask) > m) 
           m = n & Mask;
    return m;
}


Is it a correct way?
Yeah, that's right.

BTW, it may not be useful to pack the 3-bit values into an unsigned long. which can therefore only hold up to 21 of them. Maybe a vector (or possibly array) of chars would be better. Although this takes 2.5 times as much space per state, it allows you to store more than 21 states. It depends on how many states you might need to store and how much overall space they will take up.

Are you using this code? https://github.com/AdaCompNUS/despot
@dutch that is the right code and basically it shouldn't go beyond 5,6 states. I am not comfortable coding in c++ but I was forced because I wanted to use this code. Well another errors now I am running to are
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
src/solver.cpp:50:15: error: invalid use of member 'BitsPerValue' in static member function
    n = (n << BitsPerValue) | x;
              ^~~~~~~~~~~~
src/solver.cpp:57:22: error: invalid use of member 'BitsPerValue' in static member function
    for ( ; n; n >>= BitsPerValue) 
                     ^~~~~~~~~~~~
src/solver.cpp:58:18: error: invalid use of member 'Mask' in static member function
        if ((n & Mask) > m) 
                 ^~~~
src/solver.cpp:59:20: error: invalid use of member 'Mask' in static member function
           m = n & Mask;
                   ^~~~
src/solver.cpp:335:19: error: binding reference of type 'unsigned long' to value of type 'const unsigned long' drops 'const' qualifier
            store(histObs,obs_[state.state_id]);
                  ^~~~~~~
src/solver.cpp:48:44: note: passing argument to parameter 'n' here
void StarMazeProblem::store(unsigned long &n, unsigned x) {
                                           ^
src/solver.cpp:359:13: error: binding reference of type 'unsigned long' to value of type 'const unsigned long' drops 'const' qualifier
      store(histObs,obs_[state.state_id]);
            ^~~~~~~
src/solver.cpp:48:44: note: passing argument to parameter 'n' here
void Solver::store(unsigned long &n, unsigned x) {
                                           ^
In file included from src/solver.cpp:1:
src/solver.h:72:22: warning: private field 'Mask' is not used [-Wunused-private-field]
      const unsigned Mask = (1u << BitsPerValue) - 1;

I think I should change how I defined Mask and BitsPerValue in my class too, right?
Last edited on
Yeah, BitsPerValue and Mask also need to be static, so just add "static" before their declarations.
Some of those errors still seem to be related to the "const" problem, so make sure you do what helios said, too.
@dutch thanks for answering my questions. Your suggestion partially reduced the errors. As I already asked @helios, I wrote down the ObsProb function because POMDP solver use it with this given structure. Don't you think changing its type would cause a conflict and leads to more errors?

1
2
3
4
5
6
7
8
9
10
11
12
src/solver.cpp:335:19: error: binding reference of type 'unsigned long' to value of type 'const unsigned long' drops 'const' qualifier
            store(histObs,obs_[state.state_id]);
                  ^~~~~~~
src/solver.cpp:48:44: note: passing argument to parameter 'n' here
void Solver::store(unsigned long &n, unsigned x) {
                                           ^
src/solver.cpp:359:13: error: binding reference of type 'unsigned long' to value of type 'const unsigned long' drops 'const' qualifier
      store(histObs,obs_[state.state_id]);
            ^~~~~~~
src/solver.cpp:48:44: note: passing argument to parameter 'n' here
void Solver::store(unsigned long &n, unsigned x) {
                                           ^
Last edited on
I wrote down the ObsProb function because POMDP solver use it with this given structure.
Then you have to figure out how to implement ObsProb() in such a way that it doesn't involve changing the state of the object.
Either way you're getting errors. You seem to be in a catch-22. Generally this indicates a design issue. I don't really have the time to look into how you should fix it. I would need to see your full code and also read up on the other code you are using.

It's possible you could make histObs static, too. It depends on how exactly you are using this class.
Last edited on
@dutch exactly I fix some problem another one pops up. Well, if someone professional could take a look at my whole code everything could be easily solved.
Topic archived. No new replies allowed.