using a function template to return vector

Hi,

I'm trying to figure out how to use a generic function template to simply return a vector array of ints or doubles or whatever else I want.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
template <typename  T>
std::vector<T> myVec(T s, T e, T v)
{
std::vector<T> C;
for (s; s <= e; s += v)	{ C.push_back(s); }
return C;
}

main()
{
 std::vector<int> V = myVec<int>(1,10,1);
}

It gives me a linker error to this function when it compiles. Can anyone please help?

Thank you.
Last edited on
Works for me.
Maybe clean your project?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <vector>
#include <iostream>
template <typename  T>
void printFunction(const std::vector<T> &V) {
    for(auto const item: V){
        std::cout << item << std::endl;
    }
}

template <typename  T>
std::vector<T> myVec(T s, T e, T v)
{
    std::vector<T> C;
    for (s; s <= e; s += v)	{ C.push_back(s); }
    return C;
}

int main()
{
    std::vector<int> V = myVec<int>(1,10,1);
    std::vector<float> F = myVec<float>(11,20,1);
    std::vector<long long> L = myVec<long long >(21,30,2);

    printFunction (V);
    printFunction (F);
    printFunction (L);

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
23
25
27
29
Last edited on
Topic archived. No new replies allowed.