[英]Print Function in Linked List c++
this is what I'm coding:这就是我正在编码的内容:
template<typename T>
inline void LinkList<T>::print() const
{
for (Iterator<int> iter = begin(); iter != end(); ++iter)
{
std::cout << *iter << std::endl;
}
return;
}
and it's saying that this line:它是说这条线:
(Iterator<int> iter = begin(); iter != end(); ++iter)
has this error:有这个错误:
`const Iterator<T> LinkList<T>::begin(void)' cannot convert `this` pointer from `const LinkList<int>` to `LinlList<int>&`
By the way:顺便一提:
template<typename T>
inline const Iterator<T> LinkList<T>::begin()
{
return m_first;
}
template<typename T>
inline const Iterator<T> LinkList<T>::end()
{
return m_last;
}
this is so that I can print out what I have in my list.这样我就可以打印出我列表中的内容。 This is for class so I HAVE to have it like this.
这是针对 class 的,所以我必须像这样。
Edit: so I did everything y'all suggested to do but it gave me errors so i went back to my original code, now i have different errors: /编辑:所以我做了你们都建议做的所有事情,但它给了我错误所以我回到我的原始代码,现在我有不同的错误:/
this is for begin();这是用于开始(); and end();
和结束();
return
:cannot convert from Node<T>
to Iterator<T>
return
:无法从Node<T>
转换为Iterator<T>
I don't exactly know what other information y'all need to help y'all out.the values for m_last and m_first is a Node我不完全知道你们还需要什么其他信息来帮助你们。m_last 和 m_first 的值是一个节点
everyone in my class seems to have the same issue however apparently the code does have a right answer because the teacher has a working code.我的 class 中的每个人似乎都有同样的问题,但显然代码确实有正确的答案,因为老师有一个工作代码。
I solved the question!!!我解决了问题!!!
template<typename T>
inline const Iterator<T> LinkList<T>::begin()
{
Node<T> *m_first;
return m_first;
}
template<typename T>
inline const Iterator<T> LinkList<T>::end()
{
Node<T>* m_last;
return m_last;
}
granted i don't know if it works the way it is intended yet!授予我不知道它是否按预期方式工作!
Your begin()
and end()
functions are not const
therefore they cannot be invoked on const
instances of LinkList<T>
.您的
begin()
和end()
函数不是const
因此它们不能在LinkList<T>
的const
实例上调用。 If you change your begin
and end
functions as follows:如果您按如下方式更改
begin
和end
功能:
template<typename T>
inline const Iterator<T> LinkList<T>::begin() const
{
return m_first;
}
template<typename T>
inline const Iterator<T> LinkList<T>::end() const
{
return m_last;
}
You should be fine.你应该没事。
Alternatively, you can make your print function non-const but I do not recommend that.或者,您可以使您的打印 function 非常量,但我不建议这样做。 Print functions should not modify the object and so
const
on the print function makes sense.打印功能不应修改 object 等
const
打印 function 是有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.