Having trouble learning how to pass a function a parameter

Jan 16, 2012 at 10:16pm
Hi, I'm working on a project to learn how to pass one function to another one. From everything I've read it seems like I have to create a pointer to the function I want to pass and use that but I'm having trouble, I am getting two errors:

Error 1:
error C2440: '=' : cannot convert from 'int' to 'void (__cdecl *)(int)'

Error 2:
error C2664: 'Car::saveData' : cannot convert parameter 1 from 'void (__cdecl *)(int)' to 'int'


Here is my code, any suggestions or help is greatly appreciated...

============= MAIN ===========

#include <iostream>
#include <cctype>
#include <string>
#include "car.h"
using namespace std;

int main()
{
Car car1;
int g, p, c;

void (*f)(int);
f = car1.getMpg(c, p); //<---Error 1

cout << "Input miles before last fillup: ";
cin >> p;

cout << "Enter current miles: ";
cin >> c;

cout << "Input amount of gas put in: ";
cin >> g;

if (!car1.setPreviousmiles(p))
cout << "Invalid previous miles data entered." << endl;
else if(!car1.setCurrentmiles(c, p))
cout << "Invalid current miles data entered." << endl;
else if(!car1.setGallonsinput(g))
cout << "Invalid gallons data entered." << endl;
else
cout << "Your mpg is: " << car1.getMpg(c, p) << endl;

car1.saveData(f)(c); //<--Error 2

return 0;
}


=========== car.h ====================

#include <fstream>
#ifndef CAR_H
#define CAR_H
using namespace std;

class Car
{
private:
int currentmiles, previousmiles, milesdriven, gallonsinput, mpg;
// string make, model;
public:
bool setCurrentmiles(int c, int p);
bool setPreviousmiles(int p);
bool setGallonsinput(int g);
void saveData(int c);
int getMpg(int c, int p);
};
#endif



========= car.cpp ===================

#include "car.h"
#include <fstream>

bool Car::setCurrentmiles(int c, int p)
{
bool validData = true;

if (c > p)
currentmiles = c;
else
validData = false;

return validData;
}

bool Car::setPreviousmiles(int p)
{
bool validData = true;

if (p >= 0)
previousmiles = p;
else
validData = false;

return validData;
}

bool Car::setGallonsinput(int g)
{
bool validData = true;

if (g > 0)
gallonsinput = g;
else
validData = false;

return validData;
}

int Car::getMpg(int currentmiles, int previousmiles)
{
int m = currentmiles - previousmiles;
mpg = m/gallonsinput;
return mpg;
}

void Car::saveData(int c)
{
int currentmgp = ???; <--DON'T KNOW WHAT TO PUT HERE.
int currentmiles = c;

ofstream dataFile;
dataFile.open ("C:\\Documents and Settings\\Administrator\\Desktop\\mileagetracker\\car.txt");

dataFile << currentmpg << endl;
dataFile << currentmiles << endl;

dataFile.close();
}

Last edited on Jan 16, 2012 at 10:19pm
Jan 16, 2012 at 11:11pm
[code] "Please use code tags" [/code]
¿What do you want to do?
Last edited on Jan 16, 2012 at 11:12pm
Jan 16, 2012 at 11:56pm
Do you really want to pass function pointer as a parameter or just a parameter to a function?
Jan 17, 2012 at 12:03am
closed account (zb0S216C)
Such code is easily implemented:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// The function we want to pass:
void ToBeCalled();

// The function that will invoke the above function:
void Invoker(void(*)());

int main()
{
    Invoker(&ToBeCalled);
    return(0);
}

void ToBeCalled() { /*...*/ }

void Invoker(void(*TargetFunc)(/*No parameters*/))
{
    // Invoke the passed function...
    if(TargetFunc)
        TargetFunc();
}

Edit: I really should explain why you're receiving those errors.

The first error is informing you that the left-hand operand of the assignment operator cannot be converted to the same type as the right-hand operand of the assignment operator. In this case, int cannot be converted to a void; only vice-versa makes sense.

As for the second error: Your function pointer requires a function that returns void and takes a single integer as an argument. You're attempting to pass Car::saveData, which I'm assuming is a method, who's prototype doesn't qualify to be passed to the function pointer. Also, you should note that a function pointer to a method has a different prototype. More specifically, this:

<TYPE>(<CLASS_ID>:: *<POINTER_ID>)(<PARAMETERS>);

Wazzak
Last edited on Jan 17, 2012 at 12:23am
Jan 17, 2012 at 12:18am
well what I want to do is to be able to save the mpg data to a file. I was told the best way would but to pass the functions (at least that's how I understood it).
Jan 17, 2012 at 12:45am
Thank you framework, but where do each of these pieces go?
Jan 17, 2012 at 12:48am
closed account (zb0S216C)
In that case, since you're using OOP, try using inheritance. If that's out of the question because you don't know it, I would use function pointers, like such:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class BaseCar
{
    /*
     * Contents...
     */

     void Export(std::ofstream &) const;
};

void BaseCar::Export(std::ofstream &Stream) const
{
    // Save the data to 'Stream.'
}

typedef void(BaseCar:: *ActiveCar)(std::ofstream &) const;

int main()
{
    BaseCar Audi, BMW, Vauxhall;
    ActiveCar ActiveCarPtr(&BaseCar::Export);
    std::ofstream WorkingFile("SomeFile.txt");
 
    (Audi.*ActiveCarPtr)(WorkingFile);
    (BMW.*ActiveCarPtr)(WorkingFile);
    (Vauxhall.*ActiveCarPtr)(WorkingFile);

    return(0);
}


Wazzak
Last edited on Jan 17, 2012 at 12:49am
Topic archived. No new replies allowed.