Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Mac OS X上使用clang llvm3.6命令行工具时出现哈希问题_C++_Macos_Hash - Fatal编程技术网

C++ Mac OS X上使用clang llvm3.6命令行工具时出现哈希问题

C++ Mac OS X上使用clang llvm3.6命令行工具时出现哈希问题,c++,macos,hash,C++,Macos,Hash,在编译Bjourne Stroustrup提供并由我的教授编辑的头文件时遇到问题。我使用命令行工具在OSX10.10.2上编译。我的编译命令是“gcc-std=C++11*cpp”,这段代码有些地方它就是不喜欢。我在Linux和我大学的Unix服务器上运行了这个程序,没有任何问题。非常感谢您的帮助 我将包括头文件,然后是错误 /*std_lib_facilities_4.h Minimally revised for C++11 features of GCC 4.6.3 or later Wa

在编译Bjourne Stroustrup提供并由我的教授编辑的头文件时遇到问题。我使用命令行工具在OSX10.10.2上编译。我的编译命令是“gcc-std=C++11*cpp”,这段代码有些地方它就是不喜欢。我在Linux和我大学的Unix服务器上运行了这个程序,没有任何问题。非常感谢您的帮助

我将包括头文件,然后是错误

/*std_lib_facilities_4.h
Minimally revised for C++11 features of GCC 4.6.3 or later
Walter C. Daugherity        June 10, 2012 and January 9, 2014
*/

/*
simple "Programming: Principles and Practice using C++" 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.

Revised April 25, 2010: simple_error() added
*/

#ifndef H112
#define H112 201401L

#define GCC_VERSION (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)

#if GCC_VERSION >= 40603
//New C++11 headers in GCC 4.6.3 or later
#include <array>
#include <regex>
#include <thread>
#include <mutex>
#include <forward_list>
#include <ratio>
#include <tuple>
#include <chrono>
#include <random>
#endif

#include<iostream>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include<vector>
#include<algorithm>
#include<stdexcept>

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

#if GCC_VERSION >= 40603
#include <unordered_map>
#include <unordered_set>
#else
#define unordered_map hash_map
#ifdef _MSC_VER
#include <hash_map>
using stdext::hash_map;
#else
#include <ext/hash_map>
using __gnu_cxx::hash_map;

namespace __gnu_cxx {

    template<> struct hash<std::string>
    {
        size_t operator()(const std::string& s) const
        {
            return hash<char*>()(s.c_str());
        }
    };

} // of namespace __gnu_cxx
#endif //_MSC_VER
#endif //GCC_VERSION >= 40603

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

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> {
    typedef typename std::vector<T>::size_type size_type;

    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) {}
#if GCC_VERSION >= 40603
    Vector(initializer_list<T> list) : std::vector<T>(list) {}
#endif

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

    String() { }
    String(const char* p) :std::string(p) {}
    String(const string& s) :std::string(s) {}
    template<class S> String(S s) : std::string(s) {}
    String(int sz, char val) :std::string(sz, val) {}
    template<class Iter> String(Iter p1, Iter p2) : std::string(p1, p2) { }

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

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

#ifndef _MSC_VER
#if GCC_VERSION >= 40603
namespace std {

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

} // of namespace std
#else
namespace __gnu_cxx {

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

} // of namespace __gnu_cxx
#endif //GCC_VERSION >= 40603
#endif //_MSC_VER


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());
}

#if _MSC_VER<1500
// disgusting macro hack to get a range checked string:
#define string String
// MS C++ 9.0 have a built-in assert for string range check
// and uses "std::string" in several places so that macro substitution fails
#endif

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:
#undef min
#undef max

#include<iomanip>
inline ios_base& general(ios_base& b)   // to augment fixed and scientific
{
    b.setf(ios_base::fmtflags(0), ios_base::floatfield);
    return b;
}

// run-time checked narrowing cast (type conversion):
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;
}


inline int randint(int max) { return rand() % max; }

inline int randint(int min, int max) { return randint(max - min) + min; }

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

#endif //H112
/*标准图书馆设施4.h
对GCC 4.6.3或更高版本的C++11功能进行了最低限度的修改
Walter C.Daugherity 2012年6月10日和2014年1月9日
*/
/*
简单的“编程:使用C++的原理和实践”课程标题
在最初的几周内使用。
它提供最常见的标准头(在全局命名空间中)
以及最低限度的异常/错误支持。
学生:请不要试图理解标题的细节。
一切都会解释清楚的。此标题主要用于使您没有
同时理解每一个概念。
2010年4月25日修订:添加了简单错误()
*/
#ifndef H112
#定义H112 201401L
#定义GCC_版本(_GNUC_*10000+_GNUC_次要_*100+_GNUC_补丁级别)
#如果GCC_版本>=40603
//GCC4.6.3或更高版本中的新C++11标题
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#恩迪夫
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//------------------------------------------------------------------------------
#如果GCC_版本>=40603
#包括
#包括
#否则
#定义无序\u映射哈希\u映射
#ifdef硕士学位
#包括
使用stdext::hash\u映射;
#否则
#包括
使用_gnu_cxx::hash_映射;
名称空间{
模板结构哈希
{
size_t运算符()(常量std::string&s)常量
{
返回hash()(s.c_str());
}
};
}//名称空间的_gnu_cxx
#endif/\u MSC\u VER
#endif//GCC_VERSION>=40603
//------------------------------------------------------------------------------
typedef长Unicode;
//------------------------------------------------------------------------------
使用名称空间std;
模板字符串到_字符串(常量T&T)
{
ostringstream os;
os结构向量:public std::Vector{
typedef typename std::vector::size\u type size\u type;
向量(){}
显式向量(size_type n):std::Vector(n){}
向量(size_type n,const T&v):std::向量(n,v){
模板
向量(I first,I last):std::向量(first,last){
#如果GCC_版本>=40603
向量(初始值设定项列表):std::Vector(列表){
#恩迪夫
T&运算符[](无符号整数i)//而不是在(i)处返回;
{

如果(--sisie())(p)>下面的奇数球文件名的解释,我有一个脚本,代码“CKHHDR++< /COD>”,它检查C++头是否可编译,是否是幂等的。它创建了一个3行源文件,如<代码> /tMP/CHKHDR-95127.CPP < /代码>(编号是进程ID),包含:

#include HEADER /* Check self-containment */
#include HEADER /* Check idempotency */
int main(void){return 0;}
然后使用命令行对其进行编译,例如:

 /usr/bin/g++ -std=c++11 -Wall -Wextra -DHEADER=\"stdlibfacilities.h\" -c \
     /tmp/chkhdr-95127.cpp
当我用
/usr/bin/g++
(这是XCode 6.3中的
g++
,实际上是
clang++
)编译问题标题(命名为
stdlibfacilities.h
)时,我得到了错误和警告:

In file included from chkhdr-95127.cpp:1:
In file included from ./stdlibfacilities.h:59:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ext/hash_map:212:5: warning: Use of
      the header <ext/hash_map> is deprecated. Migrate to <unordered_map> [-W#warnings]
#   warning Use of the header <ext/hash_map> is deprecated.  Migrate to <unordered_map>
    ^
In file included from chkhdr-95127.cpp:1:
./stdlibfacilities.h:68:20: error: no matching function for call to object of type 'hash<char *>'
            return hash<char*>()(s.c_str());
                   ^~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ext/__hash:38:12: note: candidate
      function not viable: 1st argument ('const value_type *' (aka 'const char *')) would lose const
      qualifier
    size_t operator()(char *__c) const _NOEXCEPT
           ^
1 warning and 1 error generated.
chkhdr-95127中包含的文件中的
。cpp:1:
在包含于./stdlibfacilities.h:59的文件中:
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/ext/hash_-map:212:5:警告:使用
标头已弃用。请迁移到[-W#warnings]
#警告:不推荐使用标头。请迁移到
^
在chkhdr-95127中包含的文件中。cpp:1:
./stdlibfacilities.h:68:20:错误:对“hash”类型的对象的调用没有匹配的函数
返回hash()(s.c_str());
^~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/ext/_散列:38:12:注意:候选者
函数不可行:第一个参数('const value_type*'(又名'const char*')将丢失常量
合格者
size_t运算符()(char*__c)const_NOEXCEPT
^
生成1个警告和1个错误。
关于不可行的候选函数的注释提出了一种解决问题的方法,即替换行:

return hash<char*>()(s.c_str());
returnhash()(s.c_str());
与:

returnhash()(s.c_str());
该汇编提供了:

In file included from chkhdr-95148.cpp:1:
In file included from ./stdlibfacilities.h:59:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ext/hash_map:212:5: warning: Use of
      the header <ext/hash_map> is deprecated. Migrate to <unordered_map> [-W#warnings]
#   warning Use of the header <ext/hash_map> is deprecated.  Migrate to <unordered_map>
    ^
1 warning generated.
chkhdr-95148中包含的文件中的
。cpp:1:
在包含于./stdlibfacilities.h:59的文件中:
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/ext/hash_-map:212:5:警告:使用
标头已弃用。请迁移到[-W#warnings]
#警告:不推荐使用标头。请迁移到
^
生成1个警告。

因此,大多数问题都有一个简单的解决方法:添加缺少的
常量
。然而,理想情况下,您应该迁移标头,使其使用标准
,而不是

。不清楚您使用的是什么编译器。是
gcc
还是
clang
?是Xcode还是Macports?Apple Xcode系统提供编译一个程序
/usr/bin/gcc
,它不是真正的gcc,而是
clang
。XCode 6.3中的版本声称它的gcc版本是40201,使用代码中的公式。这可能会有问题。编译时,我收到的一条消息是“警告使用头
不推荐。迁移到
”.
return hash<const char*>()(s.c_str());
In file included from chkhdr-95148.cpp:1:
In file included from ./stdlibfacilities.h:59:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ext/hash_map:212:5: warning: Use of
      the header <ext/hash_map> is deprecated. Migrate to <unordered_map> [-W#warnings]
#   warning Use of the header <ext/hash_map> is deprecated.  Migrate to <unordered_map>
    ^
1 warning generated.