【发布时间】:2017-11-01 20:25:29
【问题描述】:
我有一个包含 20 个数字的列表(theList)。我想背诵它并有这个功能来做到这一点:
template<typename T>
T funktioner<T>::swap(list<T> &theList)
{
list<T> li;
auto start = theList.rbegin(), stop = theList.rend();
for (auto it = start; it != stop; ++it)
{
li.push_back(*it);
}
return li;
}
我在这个函数中从我的 UserInterface 调用函数:
template<typename T>
void UserInterface<T>::swap()
{
cout << func.swap(List) << endl;
}
但这不能正常工作,我收到以下错误消息:
error C2440: 'return': cannot convert from 'std::list<T,std::allocator<_Ty>>'to 'int'
error C2440: 'return': cannot convert from 'std::list<T,std::allocator<_Ty>>'to 'double'
为什么?我不知道这次我做错了什么。我认为我必须创建一个临时列表并将值推送到该列表并返回该列表,但我想我错了,我真的不擅长这个。有人能帮我吗?也许我在这里完全起床了? :O
【问题讨论】:
-
返回类型是
T,而不是list<T>。 -
好的,谢谢!我仍然不知道该怎么做,但我想我必须自己弄清楚,因为我可以看到我的问题得到了很多负面回应,所以我想我只是愚蠢。 :(
-
如果你想返回一个 T 的列表,那么 that 而不是返回一个 T。另外,整数不是双精度数。
-
@StudentLerning 错误信息有什么难以理解的地方吗?你的意图是好的,但这并不意味着你不能打错字而不去尝试阅读它们。
-
与您的问题完全无关,但仍然;为什么
list?在几乎任何情况下,vector将是优先的(在实践中表现更好)..