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

后端 未结 2 1651
感动是毒
感动是毒 2021-01-23 11:37

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

相关标签:
2条回答
  • 2021-01-23 12:19

    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.

    0 讨论(0)
  • 2021-01-23 12:36

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

    0 讨论(0)
提交回复
热议问题