Answer the question
In order to leave comments, you need to log in
Why does it give an error when trying to use a template function?
Hello, I have this task:
Write a function that takes a list of integers and a pointer to a function of type int (int), and replaces each element of the list with the result of applying the parameter function to it.
#include <iostream>
#include <ctype.h>
#include "fixedstack.h"
using namespace std;
template<typename int>
void function3(int [], int, int(*)(int));
int foo(int);
int main()
int size = 0;
int number = 0;
int i = 0;
int* array = NULL;
cout << "Enter amount of numbers: ";
cin >> size;
array = new int[size];
for (int i = 0; i < size; i++)
{
cin >> number;
array[i] = number;
}
function3(array, size, foo);
return 0;
}
template<typename int>
void function3(int array[], int size, int(*func)())
{
for (int i = 0; i < size; i++)
{
array[i] = foo(array[i]);
}
cout << "\nResult:" << endl;
for (int i = 0; i < size; i++)
{
cout << array[i] << " ";
}
cout << endl;
}
int foo(int number)
{
return number * 10;
}
Answer the question
In order to leave comments, you need to log in
In the declaration of int(*)(int), but in the implementation of int(*func)(). Mismatch.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question