使用g ++命令行和#include" std_lib_facilities.h"多个错误

时间:2016-06-15 07:59:19

标签: c++ gcc

我看到很多人都在谈论这个问题,但似乎没有人讨论我的问题。

我正在尝试编程和实践第2版文本,但是当我进入第一个程序时,Hello World!一,当使用Stroustrup支持网站上提供的std_lib_facilities.h文件时,它会返回一大堆错误:http://www.stroustrup.com/Programming/

我不介意甚至不使用该文件,但此时我也有,因为我不知道在使用该标题时我应该放置哪些内容。

我通过cygwin下载了g ++ / gcc,并使用的是Windows 10 Pro。在安装时,我手动选择了gcc / g ++,gdc和make,我相信是那些。确实,我选择了gcc / g ++和make选项。我实际上是尝试使用命令提示符编译它: g ++ -o filename sourcefile.cpp或gcc -o filename sourcefile.cpp

以下是实际文件本身,复制并粘贴, 然后是我使用的源代码, 接着是我得到的错误,包括带有键入命令的行, 然后是在命令提示符中输入的我的g ++和gcc版本:g ++ --version gcc --version

std_lib_facilities.h文件: http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

/*
   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

我使用的源代码:

#include "std_lib_facilities.h"

int main()
{
    cout << "Programming from Programming Principles and Practice Using C++.\n";
    return 0;
}

错误直到并包括我输入命令的行: (注意:无论我是否使用g ++ -o或gcc -o,似乎都会出现相同的错误。在这个例子中,碰巧我最后使用了gcc。)

c:\firstfold\sourcefold>gcc -o frompandphw fromprogprinc_practhw.cpp
In file included from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/forward_list:35:0,
                 from std_lib_facilities.h:36,
                 from fromprogprinc_practhw.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
In file included from fromprogprinc_practhw.cpp:1:0:
std_lib_facilities.h:71:8: error: expected nested-name-specifier before 'size_type'
  using size_type = typename std::vector<T>::size_type;
        ^
std_lib_facilities.h:82:24: warning: inheriting constructors only available with -std=c++11 or -std=gnu++11
  using std::vector<T>::vector; // inheriting constructor
                        ^
std_lib_facilities.h:102:8: error: expected nested-name-specifier before 'size_type'
  using size_type = std::string::size_type;
        ^
std_lib_facilities.h:121:23: error: 'hash' is not a class template
     template<> struct hash<String>
                       ^
std_lib_facilities.h:122:5: error: explicit specialization of non-template 'std::hash'
     {
     ^
std_lib_facilities.h: In member function 'std::size_t std::hash::operator()(const String&) const':
std_lib_facilities.h:125:20: error: 'std::hash' is not a template
             return hash<std::string>()(s);
                    ^
std_lib_facilities.h: In function 'int randint(int, int)':
std_lib_facilities.h:213:47: error: 'default_random_engine' does not name a type
 inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
                                               ^
std_lib_facilities.h:213:81: error: 'uniform_int_distribution' was not declared in this scope
 inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
                                                                                 ^
std_lib_facilities.h:213:106: error: expected primary-expression before '>' token
 inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
                                                                                                          ^
std_lib_facilities.h:213:107: error: expected primary-expression before '{' token
 inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
                                                                                                           ^
std_lib_facilities.h:213:107: error: expected ';' before '{' token
std_lib_facilities.h:213:116: error: expected ';' before '}' token
 inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
                                                                                                                    ^
std_lib_facilities.h:213:118: error: 'ran' was not declared in this scope
 inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
                                                                                                                      ^
std_lib_facilities.h: At global scope:
std_lib_facilities.h:222:1: error: expected unqualified-id before 'using'
 using Value_type = typename C::value_type;
 ^
std_lib_facilities.h:225:1: error: expected unqualified-id before 'using'
 using Iterator = typename C::iterator;
 ^
std_lib_facilities.h:243:1: error: 'Iterator' does not name a type
 Iterator<C> find(C& c, Val v)
 ^
std_lib_facilities.h:250:1: error: 'Iterator' does not name a type
 Iterator<C> find_if(C& c, Pred p)
 ^

g ++ / gcc的版本:

g ++(GCC)5.3.0 版权所有(C)2015 Free Software Foundation,Inc。 这是免费软件;查看复制条件的来源。没有 保证;甚至不适用于适销性或特定用途的适用性。

gcc(GCC)5.3.0 版权所有(C)2015 Free Software Foundation,Inc。 这是免费软件;查看复制条件的来源。没有 保证;甚至不适用于适销性或特定用途的适用性。

1 个答案:

答案 0 :(得分:0)

我猜你需要c++11,所以你的编译命令应该是: g++ -std=c++11 -o frompandphw fromprogprinc_practhw.cpp

看看它是否有效。