Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I included std_lib from Stroustrup's web-site My code is:

#include "c:\Users\theresmineusername\Documents\Visual Studio 2017\std_lib_facilities.h"

int main()
{
    cout << "Hello, World!\n";
    return 0;
}

So I got 2 errors:

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 Studio\2017\Community\VC\Tools\MSVC\14.10.24930\include\hash_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 studio\2017\community\vc\tools\msvc\14.10.24930\include\hash_map

From the beginning of book there using "std_lib_facilities.h" and in everything but pathway of this std_lib my code is equivalent to the book. Can someone explain what it mean? Can's go next through the book with it.

like image 230
crx Avatar asked Feb 06 '23 01:02

crx


2 Answers

std_lib_facilities.h is a header file written by Stroustrup himself which can be downloaded from www.stroustrup.com. It is not a standard C++ header but apparently reflects Stroustrup's idea of hiding even the simplest complexities of the language from total beginners in their first few weeks.

As the header file itself says:

This header is primarily used so that you don't have to understand every concept all at once.

I personally think that this is a very bad idea, with all due respect for Bjarne Stroustrup who is a greater genius than I could ever aspire to be.

The header file is full of things considered bad programming style (especially using namespace std;, which should never be used at global scope in a header file, or deriving from standard container classes). It also caters to outdated compilers which may not yet support certain "newer" features of C++ correctly, using a lot of ugly preprocessor directives.

It seems that the header file itself is quite outdated already (the one I linked to is 7 years old), and I'm not sure if Stroustrup ever updated it.

One of the preprocessor directives makes your compiler incorrectly include <hash_map>, when it should be <unordered_map>. Of course, that's absurd, because your program just wants to print a hello world message and is not even interested in hash maps.

Here's how your program should look like in correct C++:

#include <iostream>

int main()
{
    std::cout << "Hello, World!\n";
    return 0;
}

(Note that the return 0; is optional in main.)

If, however, you want to keep going with the std_lib_facilities.h learning aid provided by Stroustrup which you have to unlearn in a few weeks anyway, then do what the error message itself says: define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS.

The quickest way to do so is with a #define in the source code:

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
#include "c:\Users\theresmineusername\Documents\Visual Studio 2017\std_lib_facilities.h"

int main()
{
    cout << "Hello, World!\n";
    return 0;
}

When the times comes, throw it away together with the #include for std_lib_facilities.h.

like image 199
Christian Hackl Avatar answered Feb 07 '23 17:02

Christian Hackl


The idiomatic way to write this in c++ is:

#include <iostream>

int main() {
    std::cout << "Hello World!\n";
}

The header file from the book examples shouldn't be used.

like image 30
πάντα ῥεῖ Avatar answered Feb 07 '23 17:02

πάντα ῥεῖ