HashTable Insertion not working

Hey
Last edited on
Everytime I test the code, there is nothing stored. I don't know what I'm missing.


How do you test the code?

When I add a dummy player class with the minimum code so that I can compile and test the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <iostream>

class Player
{
public:
    enum Sex { MALE, FEMALE } ;

    Player(const std::string & name, Sex sex) 
        : _name(name), _sex(sex) {}

    std::string getName() const { return _name ; }
    Sex getSex() const { return _sex; }

private:
    std::string _name ;
    Sex _sex ;
};

inline std::ostream& operator<<(std::ostream & os, const Player & p )
{
    return os << "{ " << p.getName() << ", " << 
        (p.getSex()==Player::MALE?"male":"female") << " }\n" ;
}


And then use the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include "HashTable.h"

int main()
{
    HashTable h ;
    std::cout << "Size before insertion: " << h.getSize() << '\n' ;
    h.insert( "Brutus", Player("Brutus", Player::MALE)) ;
    std::cout << "Size after insertion: " << h.getSize() << '\n' ;

    std::cout << h << '\n' ;
}


I get the following output:

Size before insertion: 0
Size after insertion: 1
Slot [0]:
        { Brutus, male }

Slot [1]:
  EMPTY
Slot [2]:
  EMPTY


Which I think illustrates that you should be looping on size in operator<< and not capacity, but also clearly shows the item was added.

@cire Thank you so much for the help!
Thank worked just fine. I think my problem was when I was trying to output to the screen.
All I really had to do was:

cout << h << endl;

Thanks again.
Topic archived. No new replies allowed.