Question

I'm creating a bit array using unsigned chars for an assignment in my object oriented programming course. I was given the basic layout of what function we need. One was our Query function which is const.

this is my member data:

unsigned char* barray;         // pointer to the bit array
unsigned int arraySize;
static const unsigned int charSize = (sizeof(unsigned char) * 8);

and this is the function I'm having issues with:

bool BitArray::Query(unsigned int index)const{
unsigned int i = (Index(index)),
    p = Position(index);

unsigned int check = 1;
check = Move(check, p);

if ((check & barray[i]) == 0)
    return false;
else
    return true;

}

This function as well as an operator<< overload (also uses const keyword) get pissed off at me when I use my other functions in them (Index, Position, Move).

"IntelliSense: the object has type qualifiers that are not compatible with the member function "BitArray::Index" object type is const BitArray"

What is going on?

/* determine which element in array of chars to use */
unsigned int BitArray::Index(unsigned int n){
unsigned int index = (n / charSize);
if (((n % charSize) == 0) && (index < 0)){
    index -= 1;
}
return index;

}

/* determine index of bit of the particular char */
unsigned int BitArray::Position(unsigned int n){
unsigned int position = n;
position -= ((n / charSize) * charSize);
if (position == 0)
    position = charSize - 1;
else
    position--;
return position;

}

unsigned int BitArray::Move(unsigned int n, unsigned int m){
    return n << m;

}

Using Microsoft Visual Studio Express 2013

Was it helpful?

Solution

Those other functions need to be marked const as well. If they're not const, then you have no business calling them from a const function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top