#include <iostream>
using namespace std;
 
int main(int argc, char * argv[])
{
        hash<const char*> H;
        cout << "foo() ->" << H("foo()") << endl;
}

compilation error after
g++ -c -g main.cc
is :

main.cc: In function `int main(int, char**)':
main.cc:8: error: `hash' undeclared (first use this function)
main.cc:8: error: (Each undeclared identifier is reported only once for each 
   function it appears in.)
main.cc:8: error: syntax error before `char'
main.cc:9: error: `H' undeclared (first use this function)

why?
thanx

Dani AI

Generated

Quick diagnosis: the error happens because hash is not a built-in global template in older C++ dialects — the standard name is std::hash, added to the standard library in C++11. Writing hash<...> without the right header/namespace makes the compiler say “undeclared”. (en.cppreference.com)

Historical note (why people suggested Boost / TR1 / extensions): before C++11 the hash/unordered containers lived in TR1 or in vendor extensions (or Boost). Many toolchains exposed TR1 symbols under std::tr1:: or provided SGI/GNU extensions like __gnu_cxx::hash_map. That is the reason and were pointing to non-standard libraries. (en.wikipedia.org)

Important behavioral pitfall not yet emphasized in the thread: std::hash<const char*> hashes the pointer value (the address), not the string characters — so hashing a C string literal hashes its address, not "foo()". If the intent is to hash the text, use the string-based specializations (hash for std::string, and on modern C++ also for std::string_view) so the contents are hashed. Also note std::string_view does not own data, so lifetime must be managed. (en.cppreference.com)

Practical checklist (how to fix an OP-style snippet today):

  • Use a C++11-or-later toolchain and the standard hash (std::hash) from the standard headers.
  • Fully qualify the template as std::hash (or use the correct TR1/vendor name on older compilers).
  • If the goal is content-based hashing, operate on std::string (or std::string_view with lifetime care).
  • If stuck on an old compiler, use the TR1 namespace or the vendor ext (or Boost.Hash) as a fallback. (en.cppreference.com)

References above document the standard placement and the common historical alternatives.

Recommended Answers

All 6 Replies

>>why?
where did you declare hash ? I think it is probably declared in one of the boost libraries.

what do u mean by boost library?

Member Avatar for Member #46692

As in there is no standard hash library in c++ duh?

>why?
It depends. Where in the world do you think the hash<> template is coming from?

what do u mean by boost library?

Boost library

#include <functional> and use gcc 4.3 with the switch -std=c++0x

#include <iostream>
#include <functional>
using namespace std;
 
int main(int argc, char * argv[])
{
        hash<const char*> H;
        cout << "foo() ->" << H("foo()") << endl;
}
// g++43 -std=c++0x -Wall -c -g main.cc
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.