C 返回值列表
C++ return a list of values?
我有一个列表,其中有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;
}
我在此功能中从我的用户接口调用该函数:
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
,但是您返回的变量具有type list<T>
。我建议先使用具体类型(并且没有模板(来开发您的代码;之后,您可以将混凝土类型与占位符交换,但是具体类型可以更好地了解实际发生的事情。尝试以下代码并将其转换为模板:
list<int> swapList(list<int> &theList)
{
list<int> li;
auto start = theList.rbegin(), stop = theList.rend();
for (auto it = start; it != stop; ++it)
{
li.push_back(*it);
}
return li;
}
int main()
{
list<int> source { 1,2,3,4 };
list<int> swapped = swapList(source);
for (auto i : swapped) {
cout << i << " ";
}
}
相关文章:
- 从python中调用C++函数并获取返回值
- 为什么模板类中的对象不能返回值
- 返回值优化:显式移动还是隐式
- lock_guard是否保护返回值
- 调用CreateProcess()并获取字符串的返回值
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 查找 GCD:并非所有控制路径都返回值
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 如何从 std::thread 返回值
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 方法错误"not all control paths return a value"和方法不返回值
- 为什么教科书使用初始化列表作为操作员的返回值(复杂A)
- C 返回值列表
- 从初始化列表返回值
- 如何将c 返回值列表或其他复杂类型的QML列表
- C++如何在模板中推断可调用对象的类型(参数列表和返回值)
- 为什么"auto"在推导返回值时不能接受大括号的初始化列表?
- 方法,该方法调用所有基类的同名方法(如果存在),并将返回值保存到列表中