0

I'm trying to create a hash_set to hold the names of different files, like so:

struct eq {
    bool operator()(const char* c1, const char* c2) {
        return strcmp(c1, c2) == 0;
    }
};

int main(int argc, char* argv[])
{

    hash_set<const char*, hash<const char*>, eq> fileNames;
    return 0;
}

Which gives me a lot of compiler errors along the line of:

Error   1   error C2039 : 'bucket_size' : is not a member of 'std::hash<const char *>'  C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xhash  264 1   Tests
Error   2   error C2065 : 'bucket_size' : undeclared identifier C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xhash  264 1   Tests
Error   3   error C2039 : 'value_type' : is not a member of 'eq'    C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   419 1   Tests
Error   4   error C2146 : syntax error : missing ';' before identifier 'value_type' C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   419 1   Tests
Error   5   error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int    C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   419 1   Tests
Error   6   error C2602 : 'std::allocator_traits<_Alloc>::value_type' is not a member of a base class of 'std::allocator_traits<_Alloc>'    C :\Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0    419 1   Tests
Error   7   error C2146 : syntax error : missing ',' before identifier 'value_type' C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   242 1   Tests
Error   8   error C2065 : 'value_type' : undeclared identifier  C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   242 1   Tests
Error   9   error C2059 : syntax error : '>'    C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   242 1   Tests
...
MikeMB
  • 20,029
  • 9
  • 57
  • 102
Bob Sacamano
  • 699
  • 15
  • 39
  • What is `hash_set` actually? Did you miss to include something with your code? – πάντα ῥεῖ May 03 '15 at 11:38
  • Yes I included: #include . hash_set is the only thing I could find that visual studio recognized. – Bob Sacamano May 03 '15 at 11:39
  • 1
    Where did you get it from? `hash_set` isn't standard so it must be from some other library. How about C++11's `unorderd_set`? I don't think we can help without knowing anything about your `hash_set`. – AliciaBytes May 03 '15 at 11:41
  • `has_set`is a Visual studio type. My guess is, you wanted to actually use `std::unordered_set` – MikeMB May 03 '15 at 11:43
  • Please don't use pictures that only contain text. It makes it harder to index for searches and to read for visually impaired people. FWIW, even on a modern monitor the text above is unreadable to me. – Ulrich Eckhardt May 03 '15 at 11:51
  • While Ulrich Eckhardt is correct, that you should post your errors as text instead of a picture (You can copy-paste the error list from VS), please don't remove essential information from your question, after you got an answer. Just accept the correct answer instead. – MikeMB May 03 '15 at 12:17

1 Answers1

2

hash_set is a deprecated type from Visual Studio's STL-extension. It requires different template parameters, than what you provide.

What you should actually use (and what will (more or less) work with your parameters) is std::unordered_set:

#include <cstring>
#include <unordered_set>

using namespace std;

struct eq {
    bool operator()(const char* c1, const char* c2) {
        return strcmp(c1, c2) == 0;
    }
};

int main(int argc, char* argv[])
{
    unordered_set<const char*, hash<const char*>, eq> fileNames;
    return 0;
}

Aside from that I would highly recommend to use std::string instead of const char*, which would reduce your code to:

#include <unordered_set>
#include <string>

int main(int argc, char* argv[])
{
    std::unordered_set<std::string> fileNames;

}

Also see this this question, why it's a bad idea to use const char* as a key for std::unordered_map. Essentially you would also have to provide your own hash function and take care of allocation and deallocation of your keys.

Community
  • 1
  • 1
MikeMB
  • 20,029
  • 9
  • 57
  • 102
  • @BobSacamano: And what is the problem? I bet it has to do with using `const char*` as a key type (instead of std::string) because that introduces all kinds of additional problems you have to take care of. – MikeMB May 03 '15 at 12:10