我刚刚开始使用编程学习 C++:使用 C++ 的原则和实践。那本书告诉我使用一个头文件来为我设置东西。有问题的头文件位于http://www.stroustrup.com/Programming/std_lib_facilities.h
我正在尝试一个要求我写一个初筛的练习。我有以下程序:
#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;
}
但是当我在我的计算机上编译时,g++ primes.cpp
我得到以下输出:
~/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);
^
我已经尽力在网上找到这个问题的答案,但我就是不明白这条消息告诉我我做错了什么!请有人好心给我指出正确的方向吗?
谢谢你。