【发布时间】:2021-11-20 15:04:15
【问题描述】:
编译我的 C++ 程序时出现以下错误:
error: no matching function for call to 'std::vector<ChainingTable<int>::Record, std::allocator<ChainingTable<int>::Record> >::push_back(ChainingTable<int>::Record*)'
324 | vector_.push_back(new Record(key, value));
错误来自以下行:
template <class TYPE>
bool ChainingTable<TYPE>::update(const std::string &key, const TYPE &value)
{
if (!keyExists)
{
vector_.push_back(new Record(key, value));
}
}
这是为类定义的:
class ChainingTable : public Table<TYPE>
{
struct Record
{
TYPE data_;
std::string key_;
Record(const std::string &key, const TYPE &data)
{
key_ = key;
data_ = data;
}
};
std::vector<std::vector<Record>> records_;
int capacity_; // capacity of the array
完整代码:
int sz = numRecords();
bool rc = true;
std::hash<std::string> hashFunction;
size_t hash = hashFunction(key);
size_t idx = hash % capacity_;
std::vector<Record> vector_ = records_[idx];
bool keyExists = false;
for (int i = 0; i < vector_.size(); i++)
{
if (vector_[i].key_ == key)
{
vector_[i].data_ = value;
keyExists = true;
}
}
if (!keyExists)
{
vector_.push_back(new Record(key, value));
}
这可能是什么原因?
【问题讨论】:
-
您正在尝试推送一个指针(由
new返回),但看起来向量已初始化为使用对象。 -
什么是类型变量
vector_?我的意思是你是怎么定义的。你能告诉我们吗。上面给出的 sn-ps 中似乎没有vector_的定义。 -
@JohnnyMopp,我添加了完整的代码。很抱歉造成混乱