不理解 VS 中 HelloWorld 的错误,当包含std_lib来自 Stroustrup 的书时

Don't understand errors on HelloWorld in VS when included std_lib from Stroustrup's book

本文关键字:来自 lib Stroustrup std VS HelloWorld 错误 不理解 包含      更新时间:2023-10-16

我包括Stroustrup的Web站点的std_lib我的代码是:

#include "c:UserstheresmineusernameDocumentsVisual Studio 2017std_lib_facilities.h"
int main()
{
    cout << "Hello, World!n";
    return 0;
}

所以我有2个错误:

1. E1574    статическое объявление не удалось по причине "<hash_map> is deprecated and will be REMOVED. Please use <unordered_map>. You can define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS to acknowledge that you have received this warning."   ConsoleApplication2 c:Program Files (x86)Microsoft Visual Studio2017CommunityVCToolsMSVC14.10.24930includehash_map
2. C2338    <hash_map> is deprecated and will be REMOVED. Please use <unordered_map>. You can define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS to acknowledge that you have received this warning.  ConsoleApplication2 c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.24930includehash_map

从书籍开头,使用" std_lib_facilities.h",除了此std_lib的路径外,我的代码等同于本书。有人可以解释这是什么意思吗?可以通过书籍下一本书。

std_lib_facilities.h是由stroustrup本人撰写的标头文件,可以从www.stroustrup.com下载。它是不是标准的C 标题,但显然反映了STROUSTRUP在最初几周内将语言最简单的复杂性隐藏在总数中的最简单复杂性的想法。

正如标头文件本身所说:

此标头主要使用,因此您不必了解 每一个概念一次。

i 个人认为这是一个非常糟糕的主意,对Bjarne Stroustrup的所有应有的尊重,他是一个比我所渴望的更大的天才。

标题文件充满了被认为是不良编程样式的事物(尤其是using namespace std;在标题文件中的全局范围中使用,或从标准容器类中派生)。它还使用过时的编译器,这些编译器可能尚未使用大量丑陋的预处理器指令正确地支持C 的某些"较新"功能。

看来,标头文件本身已经过时了(我链接到7年的历史了),我不确定STROUSTRUP是否已更新。

预处理器指令之一使您的编译器不正确地包括<hash_map>,何时应为<unordered_map>。当然,这很荒谬,因为您的程序只是想打印一个Hello World消息,甚至对Hash Maps都不感兴趣。

这是您的程序在正确 C 中的外观:

#include <iostream>
int main()
{
    std::cout << "Hello, World!n";
    return 0;
}

(请注意,return 0;main中是可选的。)

但是,如果您想继续使用STROUSTRUP提供的std_lib_facilities.h学习辅助工具,无论如何您必须在几周内取消学习,然后执行错误消息本身所说的内容:定义_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS

最快的方法是使用源代码中的#define

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
#include "c:UserstheresmineusernameDocumentsVisual Studio 2017std_lib_facilities.h"
int main()
{
    cout << "Hello, World!n";
    return 0;
}

当时间到来时,将其与std_lib_facilities.h的CC_11一起扔掉。

在C 中写这篇文章的惯用方法是:

#include <iostream>
int main() {
    std::cout << "Hello World!n";
}

不应使用本书示例中的标题文件。