0

Hey so I'm trying to pass a hash table as a parameter in c++, when I call the function that I am trying to run i get an error message that i do not understand.

So this is the function:

string getRandomKey(int tableNumber, int tableSize, HashTable<string>* table, int random){
    random *= rand() % tableSize + 1;
    string randKey = to_string(tableNumber) + to_string(random);

    if((table->find(randKey)) == true){
        cout << "Key: " << randKey << " found ";
        return randKey;
    }
    return "";  
}

This is by no means the final version I'm just trying to test it. Some context is that I have a couple of hash tables, and a separate integer variable that has the number of elements that i have predetermined. The keys are set to be one of the random numbers.

Anyway so here is where I call the function:

table1->print(getRandomkey(1, sizes[2], table1*, 1));

And I get this error:

error: expected expression
    table1->print(getRandomKey(1, sizes[2], table1*, 1));
                                                   ^
1 error generated.

So, I'm not sure what I need to change or if I messed something up somewhere else. Thanks for any help you guys can give!

Todor Balabanov
  • 376
  • 3
  • 6
  • 17
Geedubs123
  • 69
  • 8
  • What is `table11`? Is it a `HashTable*`? Or is it a `HashTable**`? Perhaps you should take some time [to read a good beginners book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) about pointers and the dereference operator? – Some programmer dude Dec 09 '17 at 23:49
  • Also, what is the reason to use a pointer instead of e.g. a *reference*? Preferably a reference to a `const` since you don't modify the table. – Some programmer dude Dec 09 '17 at 23:50
  • table1 is a HashTable*. I forgot about references, do you think that would work better? – Geedubs123 Dec 09 '17 at 23:52
  • "table11*" is not valid C++. What do you expect to accomplish, here? Since the corresponding parameter is a pointer, you have to either pass a pointer here, or create a pointer to an existing object using the `&` operator. – Sam Varshavchik Dec 09 '17 at 23:54
  • Pointers should be avoided as much as possible. And in modern C++ you can almost do that, with the major exception of polymorphism. – Some programmer dude Dec 09 '17 at 23:57
  • I should have mentioned that in my actual code i didnt actually call it table1, that must have been a typo when i was changing it. So youre saying i should get rid of the number and use a reference &? – Geedubs123 Dec 09 '17 at 23:58
  • also, the actual character that the compiler had the ^ under was the comma in the function call , – Geedubs123 Dec 09 '17 at 23:59
  • You know there's an *edit* link below the tags? – Some programmer dude Dec 10 '17 at 00:02
  • Oops, i edited it now. So just to be completely clear, you recommend using a reference in the function call and in the function keep the pointer? Or what combination of that would you say? Thanks for your help regardless by the way – Geedubs123 Dec 10 '17 at 00:11
  • I recommend that [you read a good C++ book that explains what pointers and references are, and how to use them](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). stackoverflow.com itself is not a tutorial site. – Sam Varshavchik Dec 10 '17 at 00:52

1 Answers1

1

It appears that table1 is a pointer to a HashTable so when you make the call to getRandomKey the term table1* should just be table1.

table1->print(getRandomkey(1, sizes[2], table1, 1));

Richard Johnson
  • 612
  • 1
  • 9
  • 20