0

I don't know how to use hash_set in C++. I'm incredibly new to this language so I don't understand how to do a lot of things. How do I use the SGI hash_set extension so the compiler finally compiles without error? Here is my header file:

#ifndef _GAME1_H
#define _GAME1_H

#include "card.h"
#include "deck.h"
#include <ext/hash_set>

const unsigned int TRIALS = 10;

class Game1 {

private:
    // Card::Value is defined in card.h as a public enum:
    // enum Value { NullCard, Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
    std::hash_set<Card::Value> *map; // does this really need to be a pointer?

public:
    Game1();
    bool isPair();
    bool isFlush();
    void returnToDeck();
};

#endif

When I try to compile I get:

In file included from game1.cpp:9:
game1.h:13: error: using-declaration for non-member at class scope
game1.h:13: error: expected `;' before '<' token
make: *** [game1.o] Error 1
  1. I don't know what "using-declaration for non-member at class scope" means.
  2. Why is the compiler complaining that "expected `;' before '<' token" when I'm basically following the same example as SGI has on their site?
  3. I am using gcc 3.4.6 so I cannot use unordered_set
  4. I have looked at simple C++ hash_set example but I don't understand why they're using hash<int> H; is this relevant?

I'm at a stalemate since I literally cannot figure this out after hours of consulting google.

Community
  • 1
  • 1
djthoms
  • 3,026
  • 2
  • 31
  • 56
  • 6
    "*I am using gcc 3.4.6*" Dear god... – ildjarn Nov 12 '14 at 21:57
  • @ildjarn I know... it's on my school server so there's not much I can do about that – djthoms Nov 12 '14 at 21:57
  • 1
    why not using std::set? – dynamic Nov 12 '14 at 22:00
  • 2
    If this is an assignment, tell your teacher they need to update. `hash_set` was deprecated long ago. We use `unordered_set` today, which is standardized and comes with the latest GCC. – defube Nov 12 '14 at 22:01
  • @dynamic does `std::set` have the same time complexity as a `hash_set`? Based on what I see [here](http://www.cplusplus.com/reference/set/set/) this is the case. All I need to do is look through each card and see if a pair or flush occurs. Pretty easy and with some type of hash set I can do it in O(n) time... – djthoms Nov 12 '14 at 22:03
  • 1
    @djthoms No, `std::set` is a binary tree, so it will be `O(lg N)` for lookup. – Barry Nov 12 '14 at 22:09
  • Are you sure that `Card::Value` is a declared in the `public` scoped in `card.h` ? – StarPilot Nov 12 '14 at 22:46
  • @StarPilot yep, yep. It's kind of hard to see but I added that in the comments of my header file – djthoms Nov 12 '14 at 22:54

1 Answers1

0

I believe you should declare map as

// answering your other question, most likely it doesn't have to be a pointer.
__gnu_cxx::hash_set<Card::Value> map;

(According to the hash_set source)

Also, map is not a good name for a variable since it's the name of a standard class. Though it shouldn't be a cause for compilation errors here.

Anton Savin
  • 40,838
  • 8
  • 54
  • 90