str lib facilities h file from Stroustrup s Programming Principles and Practice Using C generating errors

0 votes

When I try to compile the first programme supplied in the text, the str lib facilities.h file generates problems.

The str lib facilities.h file contains the following code: 

/*
std_lib_facilities.h
*/

/*
simple "Programming: Principles and Practice using C++ (second edition)" course header to
be used for the first few weeks.
It provides the most common standard headers (in the global namespace)
and minimal exception/error support.

Students: please don't try to understand the details of headers just yet.
All will be explained. This header is primarily used so that you don't have
to understand every concept all at once.

By Chapter 10, you don't need this file and after Chapter 21, you'll understand it

Revised April 25, 2010: simple_error() added

Revised November 25 2013: remove support for pre-C++11 compilers, use C++11: <chrono>
Revised November 28 2013: add a few container algorithms
Revised June 8 2014: added #ifndef to workaround Microsoft C++11 weakness
Revised Febrary 2 2015: randint() can now be seeded (see exercise 5.13).
Revised August 3, 2020: a cleanup removing support for ancient compilers
*/

#ifndef H112
#define H112 080315L


#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>

//------------------------------------------------------------------------------


typedef long Unicode;

//------------------------------------------------------------------------------

using namespace std;

template<class T> string to_string(const T& t)
{
    ostringstream os;
    os << t;
    return os.str();
}

struct Range_error : out_of_range { // enhanced vector range error reporting
    int index;
    Range_error(int i) :out_of_range("Range error: " + to_string(i)), index(i) { }
};


// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {
    using size_type = typename std::vector<T>::size_type;

/* #ifdef _MSC_VER
    // microsoft doesn't yet support C++11 inheriting constructors
    Vector() { }
    explicit Vector(size_type n) :std::vector<T>(n) {}
    Vector(size_type n, const T& v) :std::vector<T>(n, v) {}
    template <class I>
    Vector(I first, I last) : std::vector<T>(first, last) {}
    Vector(initializer_list<T> list) : std::vector<T>(list) {}
*/
    using std::vector<T>::vector;   // inheriting constructor

    T& operator[](unsigned int i) // rather than return at(i);
    {
        if (i<0 || this->size() <= i) throw Range_error(i);
        return std::vector<T>::operator[](i);
    }
    const T& operator[](unsigned int i) const
    {
        if (i<0 || this->size() <= i) throw Range_error(i);
        return std::vector<T>::operator[](i);
    }
};

// disgusting macro hack to get a range checked vector:
#define vector Vector

// trivially range-checked string (no iterator checking):
struct String : std::string {
    using size_type = std::string::size_type;
    //  using string::string;

    char& operator[](unsigned int i) // rather than return at(i);
    {
        if (i<0 || size() <= i) throw Range_error(i);
        return std::string::operator[](i);
    }

    const char& operator[](unsigned int i) const
    {
        if (i<0 || size() <= i) throw Range_error(i);
        return std::string::operator[](i);
    }
};


namespace std {

    template<> struct hash<String>
    {
        size_t operator()(const String& s) const
        {
            return hash<std::string>()(s);
        }
    };

} // of namespace std


struct Exit : runtime_error {
    Exit() : runtime_error("Exit") {}
};

// error() simply disguises throws:
inline void error(const string& s)
{
    throw runtime_error(s);
}

inline void error(const string& s, const string& s2)
{
    error(s + s2);
}

inline void error(const string& s, int i)
{
    ostringstream os;
    os << s << ": " << i;
    error(os.str());
}


template<class T> char* as_bytes(T& i)  // needed for binary I/O
{
    void* addr = &i;    // get the address of the first byte
    // of memory used to store the object
    return static_cast<char*>(addr); // treat that memory as bytes
}


inline void keep_window_open()
{
    cin.clear();
    cout << "Please enter a character to exit\n";
    char ch;
    cin >> ch;
    return;
}

inline void keep_window_open(string s)
{
    if (s == "") return;
    cin.clear();
    cin.ignore(120, '\n');
    for (;;) {
        cout << "Please enter " << s << " to exit\n";
        string ss;
        while (cin >> ss && ss != s)
            cout << "Please enter " << s << " to exit\n";
        return;
    }
}



// error function to be used (only) until error() is introduced in Chapter 5:
inline void simple_error(string s)  // write ``error: s and exit program
{
    cerr << "error: " << s << '\n';
    keep_window_open();     // for some Windows environments
    exit(1);
}

// make std::min() and std::max() accessible on systems with antisocial macros:
#undef min
#undef max


// run-time checked narrowing cast (type conversion). See ???.
template<class R, class A> R narrow_cast(const A& a)
{
    R r = R(a);
    if (A(r) != a) error(string("info loss"));
    return r;
}

// random number generators. See 24.7.

inline default_random_engine& get_rand()
{
    static default_random_engine ran;   // note: not thread_local
    return ran;
};

inline void seed_randint(int s) { get_rand().seed(s); }

inline int randint(int min, int max) { return uniform_int_distribution<>{min, max}(get_rand()); }

inline int randint(int max) { return randint(0, max); }

//inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x

// container algorithms. See 21.9.   // C++ has better versions of this:

template<typename C>
using Value_type = typename C::value_type;

template<typename C>
using Iterator = typename C::iterator;

template<typename C>
// requires Container<C>()
void sort(C& c)
{
    std::sort(c.begin(), c.end());
}

template<typename C, typename Pred>
// requires Container<C>() && Binary_Predicate<Value_type<C>>()
void sort(C& c, Pred p)
{
    std::sort(c.begin(), c.end(), p);
}

template<typename C, typename Val>
// requires Container<C>() && Equality_comparable<C,Val>()
Iterator<C> find(C& c, Val v)
{
    return std::find(c.begin(), c.end(), v);
}

template<typename C, typename Pred>
// requires Container<C>() && Predicate<Pred,Value_type<C>>()
Iterator<C> find_if(C& c, Pred p)
{
    return std::find_if(c.begin(), c.end(), p);
}

#endif //H112

This is the program that I am trying to run:

// This program outputs the message "Hello, World!" to the monitor

#include "std_lib_facilities.h"

int main() // C++ progrmas start by executing the function main
{
    cout << "Hello, World!\n"; // output "Hello, World!"
    return 0;
}

The std_lib_facilities.h file is in the same directory as my file. The errors generated are:

(base) cooper@Coopers-MacBook-Pro c++ % cd "/Users/cooper/Desktop/c++/" && g++ std_lib_facilities.h -o std_lib_facilities && "/Users/cooper/Desktop/c++/"std_lib_facilities
clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated [-Wdeprecated]
std_lib_facilities.h:72:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
        using size_type = typename std::vector<T>::size_type;
                          ^
std_lib_facilities.h:105:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
        using size_type = std::string::size_type;
                          ^
std_lib_facilities.h:225:73: error: expected '(' for function-style cast or type construction
inline int randint(int min, int max) { return uniform_int_distribution<>{min, max}(get_rand()); }
                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~^
std_lib_facilities.h:234:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Value_type = typename C::value_type;
                   ^
std_lib_facilities.h:237:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Iterator = typename C::iterator;
                 ^
4 warnings and 1 error generated.

Jun 13, 2022 in C++ by Nicholas
• 7,760 points
1,020 views

1 answer to this question.

0 votes
Switching the compiler to compile in c++11 or newer, which can be done with these commands in VSCode, is the solution.

 

The number in "-std=c++17" specifies the version in the tasks.json replacement's "args" section.

Any version newer than 11 will run the code.
answered Jun 13, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

take string input using char* in C and C++

I'm having trouble accepting a string (actually a character array) as input.  Consider the following declaration: char* s; I have to input a string ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
412 views
0 votes
1 answer

The Definitive C++ Book Guide and List

For Beginner (includes those without coding experience) Programming: ...READ MORE

answered Jun 6, 2022 in C++ by pranav
• 2,590 points
921 views
0 votes
1 answer

Why is "using namespace std;" considered bad practice?

This has nothing to do with performan ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
888 views
0 votes
1 answer

Using getline() in C++

If you use getline() after cin >> anything, you must first flush the newline character from the buffer.  You can achieve this by using the cin.ignore() It would be something like this: string messageVar; cout ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
901 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
689 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,390 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,451 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,740 points
1,857 views
0 votes
1 answer

C++ code file extension? What is the difference between .cc and .cpp [closed]

GNU GCC recognizes all of the following ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
1,512 views
0 votes
1 answer

What does Tokens do and why they need to be created in C++ programming?

Tokenization is essential in determining what a programme does.  What Bjarne is referring to in respect to C++ code is how tokenization rules alter the meaning of a programme.  We need to know what the tokens are and how they are determined.  Specifically, how can we recognise a single token when it comes among other characters, and how should tokens be delimited if  there is ambiguity? Consider the prefix operators ++ and +, for example. Assume we have just one token + to deal with.  What does the following excerpt mean? int i = 1; ++i; Is the above going to apply unary + on i twice with + only? Or will it only increase it once? Naturally, it's vague.  We require an additional token, thus ++ is introduced as its own "word" in the language. But there is now another (though minor) issue.  What if the programmer just wants to use unary + twice without incrementing?  Rules for token processing are required.  So, if we discover that a white space is always used as a token separator, our programmer may write: int i ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
954 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP