0
我想使用find函數從我的哈希表中返回數組@element
的地址。不過,我收到編譯器錯誤:如何創建一個指向數組類成員的指針?
QuadraticProbing.cpp:134:59: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
return isActive(currentPos) ? wordElement : ITEM_NOT_FOUND;
基本上,我只是想返回一個指向@element
。所以我試着創建一個指針wordElement
到@element
並試着返回wordElement
。但那並不奏效。這裏是我的代碼片段,我無法弄清楚如何在HashEntry中獲得指向@element
的指針。
//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;
}
我們不知道ITEM_NOT_FOUND是什麼,但無論如何設計都不可行。 'array [currentPos] .element'是一個'char'數組。該函數試圖返回一個'HashedObj&'。這是行不通的。你爲什麼要設計一個特殊的「非價值」價值?你知道[十億美元的錯誤](http://lambda-the-ultimate.org/node/3186)是什麼嗎? –