0

I am trying to return the address of an array @element from my Hash Table using the find function. However, I am getting the compiler error:

QuadraticProbing.cpp:134:59: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
             return isActive( currentPos ) ? wordElement : ITEM_NOT_FOUND;

Basically, I just want to return a pointer to @element. So I tried creating a pointer wordElement to @element and tried returning wordElement. But that didn't work. Here is a snippet of my code, I can't figure out how to get a pointer to @element in HashEntry.

//Main
int main()
{
    QuadraticHashTable<char*> table(100);
    table.insert("HELLO WORLD");
    if (table.find(document[i]) == NULL))
        cout << "OH NO!";
}

//Class that has element that I want to return in find.
template <class HashedObj>
class QuadraticHashTable
{
  public:
    QuadraticHashTable()

    const HashedObj & find( const HashedObj & x ) const;

    enum EntryType { ACTIVE, EMPTY, DELETED };
  private:
    struct HashEntry
    {
        char element[20];
        EntryType info;


        HashEntry( const HashedObj & e = HashedObj( ), EntryType i = EMPTY )
          : info( i ) 
          {
            if (e != NULL)
                strcpy(element, e);
          }
    };
        vector<HashEntry> array;

//Find Function
    template <class HashedObj>
    const HashedObj & QuadraticHashTable<HashedObj>::find( const HashedObj & x ) const
    {
        int currentPos = findPos( x );
        const char * wordElement = array[currentPos].element;
        return isActive( currentPos ) ? wordElement : ITEM_NOT_FOUND;
    }
1
  • We have no idea what ITEM_NOT_FOUND is, but the design is not viable regardless. array[currentPos].element is a char array. The function tries to return a HashedObj &. This cannot work. And why are you designing in a special "not-a-value" value? Do you know what the billion dollar mistake is? Commented Feb 26, 2017 at 8:20

1 Answer 1

2
QuadraticHashTable<char*> table(100);
table.insert("HELLO WORLD");

A HashedObject is a char*

You pass "HELLO WORLD" to insert, which expects a const HashedObject&.

The const there applies at the top level, so it is a char* const& not a const char*& (which would be a different error).

Given that your entries are basically char[20] and how you wrote the entry, this code only works if HashedObjects are raw C strings. The template parameter is pointless as written. So there is that.

But char const* as the template parameter is the other way to make your code compile. But really, a template that works with exactly one type is pretty pointless.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.