使用自定义C ++头文件时的编译时错误 - " std_lib_facilities.h"

时间:2015-05-26 20:10:08

标签: c++ visual-studio visual-c++

我是C ++的新手,我正在学习使用本书 - 编程原则& Bjarne Stroustrup使用C ++练习,第1版。作者为每个程序使用头文件std_lib_facilities.hhere) - 示例,练习或练习。

我试图从第4章 -

解决这个问题13
  

创建一个程序来查找1到100之间的所有素数。   有一种经典的方法可以做到这一点,称为" Sieve of   埃拉托塞尼&#34。你不知道那种方法,上网看看   它了。使用这种方法编写程序。

当我尝试编译我的程序时(我使用Visual Studio 2013),这里 -

// Sieve of Eratosthenes

#include "std_lib_facilities.h"

int main()
{
    int max = 100, i = 0, j = 0, total = 0;
    vector<bool> primes(max + 1, true);

    // Find primes using Sieve of Eratosthenes method
    primes[0] = false;
    primes[1] = false;
    for (i = 0; i <= max; ++i){
        if (primes[i] == true){
            for (j = 2; i * j <= max; ++j){
                primes[i * j] = false;
            }
        }
    }

    // Show primes
    cout << "\n\nAll prime numbers from 1 to " << max << " -\n\n";
    for (i = 0; i < primes.size(); ++i)
        if (primes[i] == true){
            cout << i << " ";
            ++total;
        }
    cout << "\n\nTotal number of prime numbers == " << total << "\n\n";

    keep_window_open();
    return 0;
}

它显示了我无法理解的错误 -

1>  13_4exercise.cpp
1>c:\users\i$hu\documents\visual studio 2013\projects\c++ development\c++ development\13_4exercise.cpp(23): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\i$hu\documents\visual studio 2013\projects\c++ development\c++ development\std_lib_facilities.h(88): error C2440: 'return' : cannot convert from 'std::_Vb_reference<std::_Wrap_alloc<std::allocator<char32_t>>>' to 'bool &'
1>          c:\users\i$hu\documents\visual studio 2013\projects\c++ development\c++ development\std_lib_facilities.h(86) : while compiling class template member function 'bool &Vector<bool>::operator [](unsigned int)'
1>          c:\users\i$hu\documents\visual studio 2013\projects\c++ development\c++ development\13_4exercise.cpp(11) : see reference to function template instantiation 'bool &Vector<bool>::operator [](unsigned int)' being compiled
1>          c:\users\i$hu\documents\visual studio 2013\projects\c++ development\c++ development\13_4exercise.cpp(8) : see reference to class template instantiation 'Vector<bool>' being compiled

此错误意味着什么以及如何解决此问题?

2 个答案:

答案 0 :(得分:3)

您的图书馆"std_lib_facilities.h"正在使用vector的自定义实施,但没有vector<bool>专门模板。

专门模板使用allocator<bool> vector<bool>::reference作为operator[]的返回值。

在你的情况下,它使用默认分配器从std::_Vb_reference<std::_Wrap_alloc<std::allocator<char32_t>>>返回operator[] - 因此你的问题。

答案 1 :(得分:1)

此问题与您的标题有关。

如果将此包含替换为纯标准标题,则会编译:

#include <iostream>
#include <vector>
using namespace std;  // for learning purpose

并将keep_window_open()替换为cin.get()