Programming Principles and practice using c++ "std_lib_facilities"

Hello i am reading the book Programming Principles and Practice Using C++, By Bjarne Stroustrup The Creator Of C++. He wants me to use a custom header file so i followed everything he did to make his and i get a bunch of errors i copied and pasted everything from the link i was told to go to and it still does not work. Here is the link to what i must copy & paste http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h . It doesn't work for me, i also tried videos on how to make a header file to see maybe if I'm doing something wrong i copy what they do and it still gives me the error. It could just be me most likely but i don't know, if anyone can help to tell me if its me doing something wrong or if its not please do tell thanks!

Here is the link for all his stuff with that book : http://www.stroustrup.com/Programming/

I'm using Windows 8

I'm also using Microsoft Visual c++ 2010 Express

I'm also just trying to do a simple hello world that works 100% fine without using his header file.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  1
1>  Hello, World!.cpp
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(48): error C2868: 'std::vector<_Ty,_Ax>::size_type' : illegal syntax for using-declaration; expected qualified-name
1>          c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(72) : see reference to class template instantiation 'Vector<T>' being compiled
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(48): error C2143: syntax error : missing ';' before '='
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(48): error C2238: unexpected token(s) preceding ';'
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(57): error C2061: syntax error : identifier 'initializer_list'
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(57): error C2535: 'Vector<T>::Vector(void)' : member function already defined or declared
1>          c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(52) : see declaration of 'Vector<T>::Vector'
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(79): error C2868: 'std::basic_string<_Elem,_Traits,_Ax>::size_type' : illegal syntax for using-declaration; expected qualified-name
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(79): error C2143: syntax error : missing ';' before '='
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(79): error C2238: unexpected token(s) preceding ';'
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(190): error C2143: syntax error : missing ';' before '{'
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(190): error C2275: 'std::tr1::uniform_int_distribution<>' : illegal use of this type as an expression
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(190): error C2143: syntax error : missing ';' before '}'
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(199): error C2988: unrecognizable template declaration/definition
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(199): error C2059: syntax error : 'using'
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(220): error C2143: syntax error : missing ';' before '<'
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(220): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(220): error C2988: unrecognizable template declaration/definition
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(220): error C2059: syntax error : '<'
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(228): error C2143: syntax error : missing ';' before '{'
1>c:\users\william\documents\visual studio 2010\projects\hello, world!\hello, world!\std_lib_facilities.h(228): error C2447: '{' : missing function header (old-style formal list?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Sorry for all the text really want to get this problem fixed, every if its just some simple problem, that i cant understand still i have no clue what i'm doing with this header file...
It seems like your compiler is too old to handle C++11 code properly.
Get this file: Complete collection of code fragments for the 1st edition (revised)
from the link you posted and use the right header for the right chapter.
what extension file did you save? you should download the link and paste it into codeblocks folder and include it as "../../std_lib_facilities.h"
if still does not work maybe peter was right
I'm not sure, but I think that header file makes use of C++11 features. Probably Visual c++ 2010 is too old to have support for those features. You could try updating to the most recent version of Visual c++.

(Regardless of whether or not that header file is used, the C++11 features are worth having in their own right, so that's a good enough reason to upgrade).
Ok yeah I'm using VS 2010 cause that's what he tells me to download ill try and get a upgrade and try it out.
Thank you everyone for your help, it works 100% fine now!
As I said, you are using the wrong header file. This is the one for chapter 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//
// This is a standard library support code to the chapters of the book
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//

#ifndef STD_LIB_FACILITIES_GUARD
#define STD_LIB_FACILITIES_GUARD 1

#include <iostream>

using namespace std;

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

// The call to keep_window_open() is needed on some Windows machines to prevent
// them from closing the window before you have a chance to read the output. 
inline void keep_window_open()
{
    cin.get();
}

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

#endif // STD_LIB_FACILITIES_GUARD 

Not the one you linked.
I'm sorry might of left that out I'm using the second edition, of the book is that for the second edition, or is that for the first? I only see people talk about this one that i linked. So I'm a little confused. If i don't need that header file, I'm sorry for confusing you and i thank you for going out of your way to help me.
I do not know the differences between editions, the one I have is probably the first. As the chapters progress, new things are added to the header and that is why I said you need to use the right one for the right chapter and as you can see this one is much shorter than the one you linked. I wouldn't worry about using the newer one until the older version does not work and at that time it will be clearer what the problem is.

As for VS2010, maybe it is only the express version that does not work with c++11?
Maybe, but thank you for the help you people are very fast with responding to a problem thought i wasn't going to get anyone for at least a day.
I'm at my local library so if I need to reply again I'll have to do so on cell phone so those replies will be shorter than my normal ones. Hopefully the next couple of months goes by fast so I can get my internet back.

Goldwinner wrote:
Ok yeah I'm using VS 2010 cause that's what he tells me to download ill try and get a upgrade and try it out.

Bjarne doesn't recommend a VS version. Appendix C in the second edition even uses VS2k5 as the example and simply says it should be similar with subtle difference in newer editions. His site even has files on how to get it to work in VS2010, but at no point can I find where he tells the reader to get VS2010 or an specific VS version.

@admkrk
The second edition has everything in one single file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
   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
*/

#ifndef H112
#define H112 251113L


#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) {}
#else
	using std::vector<T>::vector;	// inheriting constructor
#endif

	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 int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }

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.

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 
Last edited on
Yes he does not tell me to use you are right, but seeing this i thought i was best off using VS 2010. Since it says people might find this helpful if you are struggling with VS and the std which i was. So i guess i did miss read and thought this was the best option to use at that time. But yes after getting on VS 2013 its very easy to do the same things on that as it was on VS 2010. So yeah he doesn't tell me to do anything sorry about that he just recommending people something if they are having trouble.

A reader supplied files that you might find helpful if you are struggling with Visual Studio and std_lib_facilities.h:

How to get your #include "std_lib_facilities.h" working if you are using Visual Studio 2010 express
Installing Visual studio express 2010 and Std_lib_facilities
@BHX
That is what I figured and it was probably wrong to say that was the wrong header, but using the one for the chapter would have got him going without the errors. And like it says in the comments:
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

I found it frustrating using the headers and would have probably stuck with the book if he would have just explained the stuff as he added it instead of hiding it all in the headers, but that's just me.

It sounds like Goldwinner has it working now so all's well that ends well.
Topic archived. No new replies allowed.