简体   繁体   中英

Error message from header file in Principles and Practice using C++

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.

(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 (ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM