0

I am trying to get a function to call another function through a pointer, I have gone through some posts and have tried doing it but I keep getting errors. Print is the function that I want apply to call. Apply should print all the data when print is called.

I get the error: Variable has incompatible type void

      void print(double x) //function I want to pass
{
cout << x << endl;
} 

void apply(vector<double> data, void (*f)(double))
    { //function with pointer to other function print
           for(int i = 0; i< data.sizeof(); i++)
               f(data[i]);
        }
user2076774
  • 405
  • 1
  • 8
  • 21

4 Answers4

1

It should work with the whole code below:

#include <iostream>
#include <vector>

using namespace std;

void print(double x) //function I want to pass
{
    cout << x << endl;
} 

void apply(const vector<double>& data, void (*f)(double))
{ //function with pointer to other function print
    for(size_t i = 0; i< data.size(); i++)
        f(data[i]);
}

void test()
{
    vector<double> data;
    data.push_back(1.0);
    data.push_back(2.0);
    data.push_back(3.0);

    apply(data, print);
}
ldlchina
  • 927
  • 2
  • 12
  • 31
1

I would suggest to clamp to std::function or use and auto for such job:

void apply(const vector<double>& data, std::function<void(double)> f)
{ //function with pointer to other function print
    for(size_t i = 0; i< data.size(); i++)
        f(data[i]);
}

or

void apply(const vector<double>& data, auto f)
{ //function with pointer to other function print
    for(size_t i = 0; i< data.size(); i++)
        f(data[i]);
}

Using auto will depend on the compiler since this usage is only available after c++14 standard.

Netwave
  • 40,134
  • 6
  • 50
  • 93
0

You want something like:

void apply(const vector<double>& data, void (*f)(double) ) {
   for( int i=0; i<data.size(); ++i )
       f(data[i]);
}
NietzscheanAI
  • 966
  • 6
  • 16
0

I don't see exactly what the error is. Your code seems to work (C++11):

#include <iostream>
#include <vector>

using namespace std;

void print(double x) //function I want to pass
{
    cout << x << endl;
}

void apply(const vector<double>& data, void (*f)(double)) { //function with pointer to other function print
    for (size_t i = 0; i < data.size(); i++)
        f(data[i]);
}

int main() {
    vector<double> v{1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9};
    apply(v, print);

    return 0;
}

Result:

1.1
2.2
3.3
4.4
5.5
6.6
7.7
8.8
9.9

RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms

The only changes I made were:

  • change data.sizeof() to data.size(), but I think you would have figured that already (a vector does not have a public member function called sizeof())
  • not necessary to make it work, but good practice: the first parameter for apply is (in general) best declared as const vector<double>& data. That is you pass a reference to a data type/structure that won't be modified (for memory consuming objects that is expected to be more efficient)
Ely
  • 10,860
  • 4
  • 43
  • 64
  • If I used a typedef for the function pointer parameter, do I have to initialize the typedef to point to the function in main? – user2076774 Dec 01 '15 at 09:02
  • Not sure how your question is meant. This answer shows an example of a typedef defined function pointer: http://stackoverflow.com/a/4295495/1566187 – Ely Dec 01 '15 at 10:38
  • And according to this example you could define `typedef void (*Func)(double);` and change the signature/function to `void apply(vector data, Func f)`. The rest remains unchanged. – Ely Dec 01 '15 at 10:39