user2076774
user2076774

Reputation: 405

Passing Function Pointers to Functions, c++

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]);
        }

Upvotes: 0

Views: 83

Answers (4)

Ely
Ely

Reputation: 11152

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)

Upvotes: 0

ldlchina
ldlchina

Reputation: 955

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);
}

Upvotes: 1

Netwave
Netwave

Reputation: 42678

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.

Upvotes: 1

NietzscheanAI
NietzscheanAI

Reputation: 966

You want something like:

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

Upvotes: 0

Related Questions