I\'ve just started learning C++ using Programming: Principles and Practice using C++. That book tells me to use a header file which sets things up for me. The header file in que
From a first look, the vector of bool
seems to be the problem. Unfortunately, vectors and bool values do not go well together, as iterators are unable to return a bool&
reference. See this question for a fairly detailed explanation.
(Are you sure that's the code you're compiling? where is max
declared?)
std::vector<bool>
is a very strange beast which doesn't behave like std::vector<T>
for any other T
.
Unfortunately even though std::vector<bool>
seems like the obvious choice in your case, it forces you to deal with the oddness of std::vector<bool>
and it doesn't work with Stroustrup's Vector
class template.
The detailed explanation of the error is that Stroustrup's header re-defines vector
to refer to his Vector
template, which adds some range-checking to the element access operator (i.e. operator[]
, which is the one used when you say primes[i]
). The redefined vector
cannot be instantiated with bool
because std::vector<bool>::operator[]
does not return a normal reference, and the redefined vector
expects it to do so.