c++ - No matching function for call to object of type 'hash<char">' (Stroustrup Book Drill) -


i starting learning c++ using book, "programming: practice , principles using c++"

in first drill, gave me bit of code run , said should error , should ask else help. that's i'm doing.

the error (as shown in xcode, , within header file):

no matching function call object of type 'hash<char">'  

this .cpp file

#include "std_lib_facilities.h"  int main () {     cout << "hello, world!\n"; //output "hello, world!"     keep_window_open(); //wait character entered     return 0;  } 

the header file: (in xcode, disagreement line return hash<char*>()(s.c_str()); around line 43.

/*     simple "programming: principles , practice using c++" course header     used first few weeks.     provides common standard headers (in global namespace)     , minimal exception/error support.      students: please don't try understand details of headers yet.     explained. header used don't have     understand every concept @ once.      revised april 25, 2010: simple_error() added */  #ifndef h112 #define h112 201004l  #include<iostream> #include<fstream> #include<sstream> #include<cmath> #include<cstdlib> #include<string> #include<list> #include<vector> #include<algorithm> #include<stdexcept>  //------------------------------------------------------------------------------  #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()); //here is!!!!!!!!         }     };  } // of namespace __gnu_cxx #endif  //------------------------------------------------------------------------------  #define unordered_map hash_map  //------------------------------------------------------------------------------  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, last) :std::vector<t>(first,last) {}      t& operator[](unsigned int i) // rather 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 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 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);     } };  #ifndef _msc_ver namespace __gnu_cxx {      template<> struct hash<string>     {         size_t operator()(const string& s) const         {             return hash<std::string>()(s);         }     };  } // of namespace __gnu_cxx #endif   struct exit : runtime_error {     exit(): runtime_error("exit") {} };  // error() 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 range checked string:     #define string string     // ms c++ 9.0 have built-in assert string range check     // , uses "std::string" in several places macro substitution fails #endif  template<class t> char* as_bytes(t& i)  // needed binary i/o {     void* addr = &i;    // address of first byte                         // of memory used store object     return static_cast<char*>(addr); // treat memory bytes }   inline void keep_window_open() {     cin.clear();     cout << "please enter character exit\n";     char ch;     cin >> ch;     return; }  inline void keep_window_open(string s) {     if (s=="") return;     cin.clear();     cin.ignore(120,'\n');     (;;) {         cout << "please enter " << s << " exit\n";         string ss;         while (cin >> ss && ss!=s)             cout << "please enter " << s << " exit\n";         return;     } }    // error function used (only) until error() introduced in chapter 5: inline void simple_error(string s)  // write ``error: s�� , exit program {     cerr << "error: " << s << '\n';     keep_window_open();     // windows environments     exit(1); }  // make std::min() , std::max() accessible: #undef min #undef max  #include<iomanip> inline ios_base& general(ios_base& b)   // augment fixed , 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)); }   // match c++0x  #endif 

no copyright infringement intended

it's const problem. clang reports: candidate function not viable: 1st argument ('const value_type *' (aka 'const char *')) lose const qualifier.

try:

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

previously had:

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

but s.c_str() returns const char * , can't pass const char function that's expecting char *. see c++ const usage explanation of gory detail.

also: top-tip. in xcode when error. press command-4. open little drop down arrow next error see more information, , there's useful, amazing, life-saving, coffee saving additional information :)


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -