Reputation: 139
I'm having trouble using function pointers to implement a finite state machine. I keep getting the error:
b.cpp: In function ‘int main()’:
b.cpp:51: error: ‘have0’ was not declared in this scope
I've tried adding an & to the have0 in line 51, but that didn't do anything. I've been reading about function pointers for a hour and I still can't get it to compile. I feel my understanding of function pointers is pretty good, but clearly there is something I'm missing here. All my functions are blank because I am just trying to get it to compile right now, they will eventually be filled with logic to move through the finite state machine. Any help is appreciated. Here's my b.cpp code:
#include <iostream>
#include <string>
#include "b.h"
using namespace std;
typedef void (*state)(string);
state current_state;
void b::have0(string input)
{
if(input == "quarter"){
}
}
void b::have25(string input)
{
}
void b::have50(string input)
{
}
void b::have75(string input)
{
}
void b::have100(string input)
{
}
void b::have125(string input)
{
}
void b::have150(string input)
{
}
void b::owe50(string input)
{
}
void b::owe25(string input)
{
}
int main()
{
string inputString;
// Initial state.
cout <<"Deposit Coin: ";
cin >> inputString;
cout << "You put in a "+inputString+"." << endl;
current_state = have0;
// Receive event, dispatch it, repeat
while(1)
{
if(inputString == "exit")
{
exit(0);
}
// Pass input to function using Global Function Pointer
(*current_state)(inputString);
cout <<"Deposit Coin: ";
cin >> inputString;
cout << "You put in a "+inputString+"." << endl;
}
return 0;
}
and my b.h:
#ifndef B_H
#define B_H
#include <string>
class b{
public:
void have0(std::string);
void have25(std::string);
void have50(std::string);
void have75(std::string);
void have100(std::string);
void have125(std::string);
void have150(std::string);
void have175(std::string);
void have200(std::string);
void have225(std::string);
void owe125(std::string);
void owe100(std::string);
void owe75(std::string);
void owe50(std::string);
void owe25(std::string);
};
#endif
Upvotes: 0
Views: 923
Reputation: 490108
The have0
, have25
, etc., that you've defined are all member functions, so to get their addresses, you'd need something like: &b::have0
. Also note, however, that you can't assign that to a pointer to a function -- it's a pointer to a member function, which is roughly analogous in some ways, but definitely not the same thing (nor can one substitute for the other).
In other words, the definition you're currently using of state
won't work to hold a pointer to a member function. Likewise, your code in main
that tries to use state
won't work for a pointer to a member function.
The most obvious (and probably best, at least based on what we're seeing so far) way to deal with this is to change b
from a class to a namespace. You don't seem to be planning to create instances of b
anyway, so it looks like a namespace probably fits your needs better.
Upvotes: 1
Reputation: 42119
You've made have0
a member function of class b
, so you cannot just point to it “stand-alone”; it only exists in relation to an instance of the class (and then the signature won't match that of your function pointer, since there will be a hidden argument to pass the reference to the object itself).
The easiest fix is to remove the class b
altogether, especially since you don't seem to be using it for anything currently, i.e., header would be just:
#include <string>
void have0(std::string);
…
And the function definitions without the b::
prefix.
Upvotes: 2