问题
I am using Codeblocks 17.12 and have already set compiler settings to C++11 standard. 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". I copied it from his website and saved in "include" folder of "Mingw" folder. After that I proceeded to make a simple program:
#include<iostream>
#include "std_lib_facilities.h"
main()
{
std::cout<<"Hello world";
}
But the compiler is showing following errors and warnings:
warning: This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp] error: template-id 'do_get<>' for 'String > std::__cxx11::messages<char>::do_get(std::messages_base::catalog, int, int, const String&) const' does not match any template declaration note: saw 1 'template<>', need 2 for specializing a member function template
Also the error which is showing is in the 1971 line of the header file "locale_facets_nonio.h"
.
I tried to find out the solution to this problem in other forums, but could not find a satisfactory answer.
Some are saying we should not use this file "std_lib_facilities.h"
at all as it is using deprecated or antiquated headers.
回答1:
we should not use this file "std_lib_facilities.h" at all as it is using deprecated or antiquated headers.
You should #include
standard headers as you use them. The std_lib_facilities.h
might get out of sync.
#include<iostream>
#include "std_lib_facilities.h"
int main() {
std::cout<<"Hello world";
}
should rather be
#include<iostream>
// #include "std_lib_facilities.h" Remove this entirely!
int main() {
std::cout<<"Hello world";
}
Using more standard features like std::string
should be:
#include<iostream>
#include<string>
int main() {
std::string hello = "Hello world";
std::cout<<hello;
}
Extending further, reading the #include std_lib_facilities.h
in your books example should probably become to expand the actually necessary standard header includes for your compilable and productive code.
Here's just a default starting template as used by Coliru
#include <iostream>
#include <vector>
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
for (auto& el : vec)
{
os << el << ' ';
}
return os;
}
int main()
{
std::vector<std::string> vec = {
"Hello", "from", "GCC", __VERSION__, "!"
};
std::cout << vec << std::endl;
}
Sure you could gather up the
#include <iostream>
#include <vector>
in a separate header file, but that would be tedious to keep in sync of what you need in particular with all of your translation units.
Another related Q&A:
Why should I not #include <bits/stdc++.h>?
回答2:
There is an updated version of that file that works fine for the most recent revision of the ISO/IEC 14882 standard, namely C++17.
https://github.com/BjarneStroustrup/Programming-_Principles_and_Practice_Using_Cpp/blob/master/std_lib_facilities.h
You don't need that line:
#include<iostream>
Hope you have not quit learning C++ with that wonderful book!
回答3:
I haven't read that book you referenced, but you can try switching to <iostream>
and your program will compile without any errors.
I am guessing "std_lib_facilities.h" is more of a place holder than a realistic header.
回答4:
This is how I got C++11 to 17 working on Mac with Xcode installed. I am following the textbook 'Programming: Principles and Practice Using C++' by Bjarne Stroustrup. The following allows you to follow the textbook as Mr C++ himself wants you to. This works perfectly for me now with std_lib_facilities.h on Xcode and terminal. This is just what I did to get it working and the troubleshooting involved which can hopefully save you some time.
This is my .cpp file
// This program outputs the message "Hello, World!"
#include "std_lib_facilities.h"
int main() // C++ programs start by executing the function main
{
cout<<"Hello, World!\n"; // output "Hello, World!"
keep_window_open(); //wait for a character to be entered
return 0;
}
Stroustrup uses a custom header file called std_lib_facilities.h but for it to work I have had to make a few changes due to a couple errors that occured during compiling with the file currently on his website. It must be in the same folder as the .cpp if the code is like the above.
On Xcode you can change the C++ version in the project's build settings C++ language dialect
On Mac, I use clang++ instead of g++ to to compile files via terminal (these come with Xcode). To get terminal working with clang and g++ I had to enable developer tool permissions triggered by the command xcode-select --reset
.
You must specify the c++ version or you get a bunch of warnings e.g.
clang++ hello_world.cpp -std=c++14
./a.out
for the .h -> std:: was added before each ios_base and normal rather than curly brackets used for uniform_int_distribution
Replace your existing std_lib_facilities.h file with this:
/*
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 June 15 for defaultfloat hack for older GCCs
*/
#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 std::ios_base& defaultfloat(std::ios_base& b) // to augment fixed and scientific as in C++11
{
b.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);
return b;
}
#endif
//------------------------------------------------------------------------------
using Unicode = long;
//------------------------------------------------------------------------------
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.
default_random_engine& get_rand()
{
static default_random_engine ran;
return ran;
};
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.
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
回答5:
Using the updated header is probably the best solution. For those interested in this particular error: it seems to be caused by
// disgusting macro hack to get a range checked string:
#define string String
When <iomanip>
is included after this macro, gcc runs into errors. When it is included before, gcc compiles fine. (Note: this macro is enabled only when _MSC_VER<1500
; according to comments "MS C++ 9.0" does not get this macro.)
This is an object lesson on why hacks are not recommended. They break over time.
来源:https://stackoverflow.com/questions/51313578/std-lib-facilities-h-showing-error