Binary Vector to Decimal Conversion error

Hey everybody,
I'm storing an n bit binary number in a vector of bools and I'm not sure whats wrong with the following function. It gives me an error along the lines of vector<bool> cannot be dereferenced:

1
2
3
4
5
6
7
8
9
10
11
float calcDecVal( vector<bool> binary, float res, int size)
{
	float decValue = 0;

	for (int count = 1; count <= size; count ++)
	{
		decValue += res*((binary[count-1]*2)^count); 
	}

	return decValue;
}


res is the resolution for an A to D converter for anyone who's interested. Does this have something to do with passing by reference vs value?
Why are you passing the size for a vector?
An std::vector knows its own size, just like all other STL containers I can think of.

So your for() loop should rather look like:

1
2
for (vector<bool>::size_type count = 1; count <= binary.size(); ++count)
    decValue += res*((binary[count-1]*2)^count);


Even this looks a bit awkward, in my opinion. Personally I would use a C++11 range-based for() loop, a bit like this:

1
2
3
4
5
size_t count=1; // std::size_t can be found in cstddef header
float decValue = 0;

for (bool b: binary)
    decValue += res * ((b*2) ^ count++);


Edit: formatting.
Last edited on
Yeah but im still getting the error that says:
Expression: vector<bool> iterator not dereferencable

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
Post more of your code.
Topic archived. No new replies allowed.