返回指针的模板函数出错 [英] Error on template functions returning pointers
问题描述
我正在尝试模拟基于指针的列表类和几个函数(我尝试返回指针的函数)正在生成错误消息。
班级:
< blockquote>尝试在ListNode之后向右移动*。所以,ListNode * List< T> :: getHead(),而不是ListNode * List< T> :: getHead()。第二个意味着该类被称为* List< T>并且该函数返回ListNode。第一个告诉编译器该类被称为List< T>并且该函数返回一个ListNode指针。
嗯......感谢您的建议,但它似乎没有什么区别。还有其他想法吗?
再次感谢!
尝试在ListNode之后向右移动* 。所以,ListNode * List< T> :: getHead(),而不是ListNode * List< T> :: getHead()。第二个意味着该类被称为* List< T>并且该函数返回ListNode。第一个告诉编译器该类被称为List< T>并且该函数返回ListNode指针。
Laharl,实际上,这里的空白是一个偏好问题 - 特别是因为类名不能以*字符开头(否则每次你说int * x时,你会得到一个错误)。
我希望,由于ListNode本身就是一个模板结构,你需要将它作为返回类型的模板:
展开 | 选择 | Wrap | 行号
I''m trying to template a pointer-based list class and several functions (the ones in which I try to return a pointer) are producing error messages.
The class:
Expand|Select|Wrap|Line Numbers解决方案Try moving the * to right after ListNode. So, ListNode* List<T>::getHead(), rather than ListNode *List<T>::getHead(). The second one implies that the class is called *List<T> and the function returns a ListNode. The first tells the compiler that the class is called List<T> and the function returns a ListNode pointer.
Hmm... Thanks for the suggestion, but it didn''t seem to make a difference. Any other ideas?
Thanks again!
Try moving the * to right after ListNode. So, ListNode* List<T>::getHead(), rather than ListNode *List<T>::getHead(). The second one implies that the class is called *List<T> and the function returns a ListNode. The first tells the compiler that the class is called List<T> and the function returns a ListNode pointer.Laharl, actually, the whitespace here is a matter of preference - especially since classnames cannot start with the * character (else every time you said int *x, you would get an error).
I expect that, since ListNode is itself a template struct, you need to treat it as a template in your return type:
Expand|Select|Wrap|Line Numbers
这篇关于返回指针的模板函数出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文登录 关闭
扫码关注1秒登录发送“验证码”获取 | 15天全站免登陆