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 question is available at http://www.stroustrup.com/Programming/std_lib_facilities.h
I'm attempting an exercise which asks me to write a prime sieve. I have the following program:
#include "std_lib_facilities.h"
void sieve_erat(int end) {
vector<bool> primes (end, true);
int final_element = sqrt(end) + 1;
for (int i=2; i<final_element; ++i)
if (primes[i])
for (int j=i*i; j<end; j += i)
primes[j] = false;
for (int p=2; p<end; ++p)
if (primes[p])
cout << p << " ";
cout << '\n';
}
int main() {
cout << "Enter the number to which I should find the primes: ";
cin >> max;
sieve_erat(max);
return 0;
}
But when I compile on my computer with g++ primes.cpp
I get the following output:
~/src/c++ $ g++ primes.cpp
In file included from /usr/include/c++/4.8.1/ext/hash_map:60:0,
from std_lib_facilities.h:34,
from primes.cpp:4:
/usr/include/c++/4.8.1/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp]
#warning \
^
In file included from primes.cpp:4:0:
std_lib_facilities.h: In instantiation of ‘T& Vector<T>::operator[](unsigned int) [with T = bool]’:
primes.cpp:36:17: required from here
std_lib_facilities.h:88:38: error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector<bool, std::allocator<bool> >::reference {aka std::_Bit_reference}’
return std::vector<T>::operator[](i);
^
I've tried my best to find the answer to this question on the web, but I just can't understand what the message is telling me I've done wrong! Please may somebody be kind enough to point me in the right direction?
Thank you.