In your code, you declare to return type T
, but the variable you return has type list<T>
. I’d suggest to develop your code with concrete types (and without templates) first; you can exchange the concrete type with placeholders afterwards, but concrete types give a better idea what actually happens.
Try the following code and translate it then to a template:
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 << " ";
}
}
solved C++ return a list of values? [closed]