如何处理模板返回
How to deal with template return?
也许是因为我是一个新人,无法定义它属于哪种问题。所以我搜索后没有找到想要的结果。
这是我的链表实现与C++模板
template<class T> struct Node
{
T value;
Node<T>* pre;
Node<T>* next;
};
template<class T> class Flist
{
private:
Node<T>* front;
Node<T>* end;
int count;
public:
Flist();
~Flist();
Flist(const Flist& C_list);
inline void Deeply_Copy(const Flist& Copylist);
bool Isempty() const;
int Listsize() const;
Node<T>& Listfront()const;
Node<T>& Listend() const;
void push_front(T N);
void push_back(T N);
void del_front();
void del_back();
Node<T>* Listfind(T x);
T ShowKey(int n);
};
template<class T> T Flist<T>::ShowKey(int n)
{
if (front == 0)
{
cout << "there is no element is the list.." << endl;
return ???;
}
Node<T>* temp = front;
while(n--)
{
temp = temp->next;
}
return temp->value;
}
函数 ShowKey(int n( 我希望它返回(而不仅仅是显示(第 n 个元素的值,但如果列表为空,我不知道要返回什么。我不想使用退出来停止该过程。有没有更友好的方法来处理这种情况?
我会将函数签名更改为
bool showKey(int n, T& value);
并使用它来设置 value 变量,如果条目存在于索引 n 处,则返回 true,如果条目不存在,则返回 false(并保持值不变(。
最好的选择可能是抛出异常; std::domain_error
或std::range_error
可能合适。
相关文章:
- 使用返回对象的函数处理错误
- 如何使用从处理程序调度的最终回调将响应异步返回给调用方on_read?
- 如何在 c++ 窗口中将参数和返回的退出值传递到批处理文件/从批处理文件获取返回的退出值
- 处理模板函数包装中的void返回
- 使用std::tie进行类似golang的错误处理,同时返回结果,是否有缺点?(C++11)
- 对clang、gcc和icc中开关枚举类返回的处理一致
- 我可以让返回类型自动处理具有相同签名但捕获不同内容的 lambda 吗?
- cudaMemcpyToSymbol只是挂起,永远不会返回.GPU 处理速度为 100%.代码在 K40 上工作正常,但
- 套接字 read() 函数在处理隧道时有时从不返回 0
- 在对原始字符串进行一些处理后返回(或转换)原始字符串
- 创建一个函数,如果元素在unordered_set中,则返回 true,如何处理模板
- 如何处理不保证返回任何内容的函数
- C :ScopeGuard vs返回支票和异常处理
- 返回处理数组的INT功能
- 尝试阅读文件时,如何处理马车返回线供稿
- 处理多个文件时,我必须在 COleDropTarget::OnDrop() 中返回哪个值
- 使用 enable_if 专门处理基类的返回类型
- 从函数安全返回和处理动态分配的内存,C++ 11
- 与不完整的Cholesky预处理的共轭梯度返回特征库的意外错误
- 返回处理父对象数据并在以后使其失效的对象