views:

99

answers:

3

I'm told to create template of function , that will take 4 arguments :

  • pointer
  • reference
  • pointer to array
  • pointer to function

How to perform this task ? I was trying :

#include <iostream>
using namespace std;

int nothing(int a)
{
    return a;
}

template<typename T> T func(int *L, int &M, char *K, int (*P)(int))
{
    cout << L << "," << M << "," << K[0] << "," << P() << endl;
    return 0;    
}

int main()
{
    int x = 3;
    int *z = &x;
    int &y = x;
    char c[3];
    int (*pf)(int) = nothing;

    cout << "some result of func" << func(z, y, c, pf) << endl;

    system("pause");
    return 0;
}

This gives me "no matching function , I guess for 'pf'. Also now I have no control over what to pass within pf or am I wrong ?

+3  A: 
sbi
so pointer to array should be just *K ?
anotr67
@anotr Yes ` ` ` `
Yacoby
and the size, you should pass the size as an additional argument
Vincent Robert
BTW, I don't think "function template" is meant as "templated function" but rather as a template on how to create a function. Depending on your level of study of C++, your professor might just be asking for a signature rather than a templated function.
Vincent Robert
+1  A: 

To help you little bit more, try to remember how the "main" function taking arguments looks like, this will help you to see how you can make a pointer to an array.

Gabriel Ščerbák
I first liked this hint, but then I remembered that this is an array _of pointers_. So I'm afraid that's not really helpful for beginners.
sbi
@sbi You are right, but it works and it is imho kind of language issue (array vs. pointer to first element).
Gabriel Ščerbák
+1  A: 

You now have some problems left...

TYPE (*P)(x) says you expect a pointer to function that takes an argument of type x - change it to an existing type.

In the expression func(z, y, c, pf(x)) you try to call the function pointer pf instead of just passing it.

Then you are calling func with parameters based on different types for the first 3 parameters, int and char, but func expects them to be based on the same type.
Try writing down with what types func will be called with and try matching that to a signature for func with TYPE being substituted to say int.

E.g. if you have the following:

template<typename T> void f(T* a, T* b);

and try to call it like this:

int* a = 0;
int* b = 0;
f(a, b);

the compiler instantiates and calls a function

void f<int>(int*, int*);

But if you do the following:

int*  a = 0;
char* b = 0;
f(a, b);

what should be called?

void f<int> (int*,  int* ); // doesn't match, 2nd argument is char*
void f<char>(char*, char*); // doesn't match, 1st argument is int*
Georg Fritzsche
Now I was trying : template<typename int> int func(int *L, int return 0; }but that gives me awful errors
anotr67
@anotr67: No, i didn't mean to change the function but to imagine how it looks when the compiler substitutes `TYPE` with e.g. `int`. That might help understand why you get *"no matching function"*.
Georg Fritzsche