Question 1 : I'm trying to use a struct with a template in function parameters and return type while declaring function.
template <typename T>
struct my_struct {
T value;
};
my_struct<T> func(my_struct<T> input_1, my_struct<T> input_2);
When I tried the above code I'm getting
error: ‘T’ was not declared in this scope
However, when declaring with concrete data_type, it compiles.
my_struct<float> func(my_struct<float> input_1, my_struct<float> input_2);
Does it mean, I've to declare function for all data types that I expect? Clearly, I misunderstood the concept here. Can someone explain this?
Update: The above mentioned issue is resolved by declaring function as template.
Question 2 : Now, when i try to invoke the function, I'm getting the below error. What am i doing wrong here?
template <typename T> struct my_struct { T value; };
template <typename X, typename Y, typename Z>
my_struct<X> func(my_struct<Y> input_1, my_struct<Z> input_2) {
my_struct<X> out;
out.value = input_1.value + input_2.value;
return out;
}
int main(int argc, char const *argv[]) {
struct my_struct<int> t1 = {1};
struct my_struct<float> t2 = {2.0};
struct my_struct<float> out = func(t1, t2);
return 0;
}
error: no matching function for call to ‘func(my_struct&, my_struct&)’