2015-04-28 34 views
2

在編譯由Bjourne Stroustrup提供並由我的教授編輯的此頭文件時遇到問題。我正在使用命令行工具在OS X 10.10.2上進行編譯。我的編譯命令是「gcc -std = C++ 11 * cpp」,並且有一些關於這個代碼的東西,它只是不喜歡。我在Linux和我的大學的Unix服務器上運行這個沒有問題。任何幫助是極大的讚賞。在Mac OS X上使用clang llvm3.6命令行工具使用哈希問題

我將包含頭文件,然後錯誤。

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

下面是錯誤

./std_lib_facilities.h:43:20: error: no matching function for call to object of 
     type 'hash<char *>' 
      return hash<char*>()(s.c_str()); 
+0

你不清楚你正在使用什麼編譯器。它是'gcc'還是'clang'?它是Xcode還是Macports? – trojanfoe

+0

Apple XCode系統提供'/ usr/bin/gcc'程序,它不是GCC,而是'clang'。 XCode 6.3中的版本聲稱其GCC版本是40201,使用代碼中的公式。這可能會把事情拋諸腦後。當我編譯時,我收到的一條消息是「警告不要使用標頭'',而是遷移到''」。 –

回答

3

通過對下面的奇數球的文件名的說明方式,我有一個腳本,chkhdr++,檢查一個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 

當我從編譯的問題(命名爲stdlibfacilities.h)與/usr/bin/g++(這是在Xcode 6.3,這又是真的clang++g++)的頭,我得到的錯誤和警告:

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. 

關於非可行的候選功能的說明提出解決問題的方式 - 即,將行:

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

有:

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. 

因此,對於大部分麻煩都有一個簡單的解決辦法;添加缺少的const。但是,理想情況下,您應該遷移標題,以便使用標準<unordered_map>而不是<ext/hash_map>