I've been trying to make a function in c++ that takes a list, performs some operation on it, and returns it. but the function won't accept std::list as a valid return type or as a parameter type.
#include <iostream>
#include <list>
std::list list_function(int n, std::list progress) {
}
int main() {
/* std::list<int> test_list = {1, 2, 3, 4, 4854};
test_list.push_back(10); // some unrelated testing
for (int x : test_list) {
std::cout << x << '\n';
} */
}
I can't find any answers online, what do I need to do?
std::list
is a template library, so you need a template argument to use that as a type.
For example:
#include <list>
std::list<int> list_function(int n, std::list<int> progress) {
return std::list<int>();
}
int main(void) {
std::list<int> a = list_function(10, std::list<int>());
return 0;
}
If you want to create a generic function, you can also make your function templated like this:
#include <list>
template<typename T>
std::list<T> list_function(int n, std::list<T> progress) {
return std::list<T>();
}
int main(void) {
std::list<int> a = list_function(10, std::list<int>());
std::list<double> b = list_function(10, std::list<double>());
return 0;
}