関数ポインターを使用して有限状態マシンを実装するのに問題があります。エラーが発生し続けます:
b.cpp: In function ‘int main()’:
b.cpp:51: error: ‘have0’ was not declared in this scope
51 行目の have0 に & を追加しようとしましたが、何もしませんでした。関数ポインターについて 1 時間読んでいますが、まだコンパイルできません。関数ポインターの理解はかなり進んでいると思いますが、ここに欠けているものがあることは明らかです。今コンパイルしようとしているだけなので、すべての関数は空白です。最終的には、有限状態マシンを移動するロジックで満たされます。どんな助けでも大歓迎です。ここに私の b.cpp コードがあります:
#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;
}
そして私のbh:
#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