简体   繁体   English

使用 C++ 的原则和实践中来自头文件的错误消息

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

I've just started learning C++ using Programming: Principles and Practice using C++.我刚刚开始使用编程学习 C++:使用 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有问题的头文件可在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:但是当我用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);
                                  ^

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?) (你确定这是你正在编译的代码? max在哪里声明?)

std::vector<bool> is a very strange beast which doesn't behave like std::vector<T> for any other T . std::vector<bool>是一个非常奇怪的野兽,对于任何其他T ,它的行为都不像std::vector<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.不幸的是,尽管std::vector<bool>在您的情况下似乎是显而易见的选择,但它迫使您处理std::vector<bool>的奇怪之处,并且它不适用于 Stroustrup 的Vector类模板。

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] ).错误的详细解释是,Stroustrup 的头部重新定义了vector来引用他的Vector模板,这对元素访问运算符(即operator[] ,这是你说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.重新定义的vector不能用bool实例化,因为std::vector<bool>::operator[]不返回正常引用,而重新定义的vector期望它这样做。

From a first look, the vector of bool seems to be the problem.乍一看, bool的向量似乎是问题所在。 Unfortunately, vectors and bool values do not go well together, as iterators are unable to return a bool& reference.不幸的是,向量和 bool 值不能很好地结合在一起,因为迭代器无法返回bool&引用。 See this question for a fairly detailed explanation.有关相当详细的解释,请参阅此问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复“Programmin原理和练习使用C ++”一书中的FLTK头文件? - How to fix the FLTK header files from 'Programmin principles and practice Using C++' book? “使用C ++的原理和实践”章节6.3.1代码错误? - “Principles and Practice Using C++” chapter 6.3.1 code error? 使用C++编程原理与实践报错:constexpr - Programming principles and practice using C++ error: constexpr 无法链接来自使用c ++的编程原理和实践的示例 - can not link examples from programming principles and practice using c++ str_lib_facilities.h 文件来自 Stroustrup 的编程:使用 C++ 生成错误的原则和实践 - str_lib_facilities.h file from Stroustrup's Programming: Principles and Practice Using C++ generating errors 第6章使用c ++的实践和原理[钻]使用令牌(计算器) - chapter 6 practice and principles using c++ [drill] using tokens (calculator) 我正在尝试从书中做一个练习:编程——使用 C++ 的原则和实践(第二版) - I'm trying to do an exercise from the book :Programming -- Principles and Practice Using C++ (Second Edition) 不理解 Bjarne Stroustrup 的第 6 章“编程:使用 C++ 的原理和实践”中的这部分代码 - Don't understand this part of code from "Programming: principles and practice using C++", chapter 6 by Bjarne Stroustrup 使用C ++的原理和实践,如何退出循环 - principles and practice using C++, how to exit a loop 使用C ++的编程原理和实践第4章钻取,第7步 - Programming Principles and Practice Using C++ Chapter 4 Drill, Step 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM