“std_lib_facilities.h”显示错误

RSS*_*SSB 6 c++ header

我正在使用 Codeblocks 17.12 并且已经将编译器设置设置为 C++11 标准。我正在学习 Bjarne Stroustrup 的书“编程 - 使用 C++ 的原则和实践”。在他的书中,他要求包括“std_lib_facilities.h”。我从他的网站复制它并保存在“Mingw”文件夹的“include”文件夹中。之后,我开始制作一个简单的程序:

#include<iostream>
#include "std_lib_facilities.h"
main()
{
    std::cout<<"Hello world";
}
Run Code Online (Sandbox Code Playgroud)

但是编译器显示以下错误和警告:

 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]

 error: template-id 'do_get<>' for 'String > 
   std::__cxx11::messages<char>::do_get(std::messages_base::catalog, int, int, const String&) const' does not match any template declaration

 note: saw 1 'template<>', need 2 for specializing a member function template
Run Code Online (Sandbox Code Playgroud)

显示的错误也出现在头文件的 1971 行中"locale_facets_nonio.h"
我试图在其他论坛中找到解决此问题的方法,但找不到满意的答案。
有人说我们根本不应该使用这个文件"std_lib_facilities.h",因为它使用的是过时或过时的标头。

Gil*_*ino 9

该文件的更新版本适用于 ISO/IEC 14882 标准的最新版本,即 C++17。

https://github.com/BjarneStroustrup/Programming-_Principles_and_Practice_Using_Cpp/blob/master/std_lib_facilities.h

你不需要那一行:

#include<iostream> 
Run Code Online (Sandbox Code Playgroud)

希望你还没有因为那本精彩的书而放弃学习 C++!


πάν*_*ῥεῖ 4

我们根本不应该使用这个文件“std_lib_facilities.h”,因为它使用已弃用或过时的标头。

您应该#include在使用标头时对其进行标准化。可能std_lib_facilities.h会不同步。

#include<iostream>
#include "std_lib_facilities.h"
int main() {
    std::cout<<"Hello world";
}
Run Code Online (Sandbox Code Playgroud)

更应该是

#include<iostream>
// #include "std_lib_facilities.h" Remove this entirely!
int main() {
    std::cout<<"Hello world";
}
Run Code Online (Sandbox Code Playgroud)

使用更多标准功能,例如std::string应该:

#include<iostream>
#include<string>
int main() {
    std::string hello = "Hello world";
    std::cout<<hello;
}
Run Code Online (Sandbox Code Playgroud)

进一步扩展,阅读#include std_lib_facilities.h书中的示例可能应该成为扩展可编译和生产代码实际必需的标准头文件。这只是Coliru
使用的默认起始模板

#include <iostream>
#include <vector>

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
    for (auto& el : vec)
    {
        os << el << ' ';
    }
    return os;
}

int main()
{
    std::vector<std::string> vec = {
        "Hello", "from", "GCC", __VERSION__, "!" 
    };
    std::cout << vec << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

当然你可以收集

#include <iostream>
#include <vector>
Run Code Online (Sandbox Code Playgroud)

在一个单独的头文件中,但是要保持您需要的同步,特别是与所有翻译单元的同步,这将是乏味的。


另一条相关问答:

为什么我不应该#include <bits/stdc++.h>?