【发布时间】:2014-09-21 21:15:50
【问题描述】:
我创建了一个名为 Map 的双链表模板化类,它接收一个名为 MapItem 的结构,该结构具有两个模板化变量(键和值),并对它们执行某些功能。到目前为止,我的所有函数都可以工作,除了这个名为 get 的函数,它接受一个键和布尔变量作为参数,或者:返回与键关联的值,如果找到键,则将布尔变量设置为 true,或者不返回任何东西并将布尔变量设置为false。当我在 main 中调用此函数时,它不断崩溃,我的思绪被错误的地方所困扰。让我知道你们的想法!
template <class keyType, class valueType>
valueType Map<keyType, valueType>::get(keyType key, bool & success) const
{
if(sizeList == 0) //if the list is empty, set success to false since there is nothing to return
success = false;
else if(sizeList == 1) //if one item, check it to see if it's the one we're looking for
{
if(head->key == key) //if it is the item, return the value
{
success = true;
return head->value;
}
else
success = false;
}
else //if the size of the list is greater than 1, increment through it
{
int i = 1;
struct MapItem<keyType, valueType> *temp = head; //store head in temp as the first item to check and increment through all the items
while(i <= sizeList)
{
if(temp->key == key) //if we found it
{
success = true;
return temp->value;
}
temp = temp->next; //get the next item
i++;
}
}
success = false;
}
这是我所说的 MapItem 结构体,它在 Map 类中用于存储项目:
template <class keyType, class valueType>
struct MapItem
{
keyType key;
valueType value;
MapItem<keyType, valueType> *prev, *next;
};
是的,我知道函数 get 没有返回语句,如果它实际上没有找到密钥,但我们的教授说它应该没问题,但此时我开始不这么想了。可以实现一个错误异常来处理这个吗?谢谢。
【问题讨论】:
-
侧面观察:为什么不返回
std::tuple而不是使用那个输出引用参数? -
你必须返回一些东西,否则程序是未定义的。你的编译器应该警告过你。
-
感谢您的回复。什么是标准:元组?在这种情况下如何使用错误处理?
-
我当然希望您只是误解了老师的意思。否则,我很抱歉。
标签: c++ pointers memory get crash