【发布时间】:2014-12-31 23:59:01
【问题描述】:
我有一个 struct 和一些 const 变量
struct HashData
{
const HashKey key;
const void* data;
HashData(const HashKey& key_, const void* data_) : key(key_), data(data_) {}
/* How to write this ?
HashData operator=(const HashData & data)
{
key = std::move(data.key);
return *this;
}
*/
};
以及我使用它的另一个类。
class HashTable
{
std::vector< std::list<HashData> > hashTable; ///< Collisions resolution by chaining.
public:
void insertAtIndex(const std::size_t index, const HashData& data) {
hashTable[index].insert(std::begin(hashTable[index]), data);
}
};
class HashTable 编译但另一个类
class OpenAddressHashTable
{
std::vector<HashData> hashTable;
public:
void insert(const HashData & data) throw() {
if (data.key == NULLKEY)
throw std::runtime_error("Do not use NullKey");
size_t iteration = 0;
do {
const size_t index = (*hashFunc)(data.key, iteration);
if (hashTable[index].key == NULLKEY) {
// space is free
// ** IMPORTANT **///// Line 131 is next line
hashTable[index] = data;
return ;
}
iteration++;
} while(iteration < hashTable.size());
throw std::runtime_error("No space left");
}
};
我收到此错误:
g++ -W -Wall -pedantic -std=c++11 hash.cpp
hash.cpp: In member function 'void OpenAddressHashTable::insert(const HashData&)':
hash.cpp:131:24: error: use of deleted function 'HashData& HashData::operator=(const HashData&)'
hash.cpp:26:8: note: 'HashData& HashData::operator=(const HashData&)' is implicitly deleted because the default definition would be ill-formed:
hash.cpp:26:8: error: non-static const member 'const HashKey HashData::key', can't use default assignment operator
std::list 我需要将数据放入我的向量中是什么?
我需要在hashTable 中使用指针吗?
【问题讨论】: