Hey. I want to simply make a function that will return a random vector of given type and sizes, but I may suck with templates & oop in c++. I tried something like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int RandomNumber() { return (rand()%100); }
template<typename T>
vector<int>* rand_vec(size_t _SIZE, T)
{
vector<T> ret_vec(_SIZE);
generate(ret_vec.begin(), ret_vec.end(), RandomNumber);
return ret_vec;
}
int main()
{
srand(time(NULL));
vector<int_fast8_t> random_vec = rand_vec(8, int_fast8_t);
for (auto X : random_vec) cout << X << "\t" ;
return 0;
}
(1) You don't pass the type of the template as a function parameter, you either pass it as a template parameter, with identifier<type>(...), or the compiler can sometimes implicitly know which type is being passed (based on the types of objects being passed in).
(2) Also, don't return a pointer to a local object. Just return the object.
(3) Despite making a vector<T>, you always attempt to return a vector<int>. Make the return type match the object you're turning.
(4) Do not create an identifier that starts with an underscore followed by a capital letter, or an identifier that has two underscores in a row (your _SIZE variable name is not allowed).
Note that you're attempting to print out "int_fast8_t" objects, which I assume ultimately become chars, so it's going to be printing out potentially non-printable ascii characters.