N
N
newmersedez2020-11-30 16:00:55
C++ / C#
newmersedez, 2020-11-30 16:00:55

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.


I tried to write this function, this is what my code looks like:
#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;
}


As a result, VS throws these errors:
Error C2672 "function3": no matching overloaded function found
Error C2783 void function3(int [],int,int (__cdecl *)(int)): cannot compose template argument for '__formal'
Error ( active) E0304 missing instances of overloaded function "function3" matching the argument list

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-11-30
@newmersedez

In the declaration of int(*)(int), but in the implementation of int(*func)(). Mismatch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question