class Stepper
{
public:
Stepper(int en,int dir, int clk, void(*f))
{
}
};
class Turret : public Stepper
{
public:
Turret(int x,int y, int z,void (*f)):Stepper(x,y,z,void(*f))
{
}
};
void TurretStep()
{
}
Turret t(2,3,4,TurretStep);
Alright so this gives me a void* is not a pointer-to-object type. All i'm trying to do is pass a void function as a parameter to my constructors.
You have 2 problems.
The first is you don't have the correct syntax for a function pointer.
The syntax for a function pointer is
return_type(*name)(arguments)
Since your function has void
return type and doesn't take any arguments, it's
void(*f)()
The second is in trying to pass the function pointer f
to the base class constructor
Here you're passing the type to the base class, when in fact you just want to pass the variable
Incorrect (both in terms of the syntax and passing the type of the argument)
Stepper(x,y,z,void(*f))
Correct (just pass f
, the function pointer variable itself
Stepper(x,y,z,f)
Here is the code with the fixes:
class Stepper
{
public:
Stepper(int en,int dir, int clk, void(*f)())
{
}
};
class Turret : public Stepper
{
public:
Turret(int x,int y, int z,void (*f)()):Stepper(x,y,z,f)
{
}
};
void TurretStep()
{
}
Turret t(2,3,4,TurretStep);
You forgot to add the empty parenthesis to the function pointer:
void(*f)()
^^
no arguments
Also, Stepper(x,y,z,void(*f))
is also wrong, because f
is just a variable (like x
, y
and z
), so why cast it to void
?
Stepper(x, y, z, f); //'f' is just like a "normal" variable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With