Bjarne Stroustrup Book - std_lib_facilities.h - won't work (unknown type name)

Viewed 1319

I am studying from Bjarne Stroustrup's book "Programming - Principles and Practice using C++". In his book he asked to include "std_lib_facilities.h". So I got part of code like this

#ifndef H112
#define H112 020215L


#include<iostream>
#include<iomanip>
#include<fstream> 
#include<sstream> 
#include<cmath> 
#include<cstdlib> 
#include<string> 
#include<list>
#include <forward_list>
#include<vector> 
#include<unordered_map>
#include<algorithm>
#include <array>
#include <regex>
#include<random>
#include<stdexcept>

//------------------------------------------------------------------------------
#if __GNUC__ && __GNUC__ < 5
inline ios_base & defaultfloat(ios_base& b)    // to augment fixed and scientific as in C++11
{
    b.setf(ios_base::fmtflags(0), ios_base::floatfield);
    return b;
}
#endif

Xcode won't compile my project and show me error

  • unknown type name in this part

  • Unknown type name 'ios_base'; did you mean 'std::ios_base'? Replace 'ios_base' with 'std::ios_base'

at

inline ios_base & defaultfloat(ios_base& b)    

and also show me error

Use of undeclared identifier 'ios_base'; did you mean 'std::ios_base'?

at

b.setf(ios_base::fmtflags(0), ios_base::floatfield);

so I changed all ios_base to std::ios_base but it won't compile my project still....

2 Answers

It seems like a bug in the file. That section is a "recent" addition, which should only run under GCC versions prior to version 5, but ios_base is used unqualified before the using-directive.

The "correct" fix is to qualify all uses of ios_base with std:::

inline std::ios_base & defaultfloat(std::ios_base& b)
{
    b.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);
    return b;
}

Alternatively, you can move the using namespace std; statement up before the code snippet in the file. That is not a pretty solution, but Bjarne's point with this header was not to demonstrate good coding practices, but to hide some complexity from beginners.

In any case, as soon as you've progressed far enough, you should stop using the header completely and do things right. The book tells you when.

T @ravnsgaard As a self-directed student was looking for a solution to this header issue for some time. This solution worked so long as I referenced C++11 during compilation: e.g. g++ -std=c++11 yourfile.cpp -o yourfile

Related