#include <iostream>
#include<string>
#include<vector>
#include<functional>
using namespace std;
void straightLineMethod();
void unitsOfActivity();
void decliningMethod();
int main()
{
using func = std::function<void()>;
string inputMethod;
string depreciation[3] = { "straight line","units of activity","declining" };
std::vector<func> functions;
functions.push_back(straightLineMethod);
functions.push_back(unitsOfActivity);
functions.push_back(decliningMethod);
cout << " Enter the method of depreciation you're using: " << depreciation[0] << ", " << depreciation[1] << " , or " << depreciation[2] << endl;
cin.ignore();
getline(cin, inputMethod);
for (int i = 0; i <functions.size() ; i++) {
while(inputMethod == depreciation[i])
{
functions[i]();
}
}
I tried researching the answer and learned about using std::function
, but I don't completely understand what it does. It's pretty hard to find an answer related to this question online. Essentially, what I want to is have the three functions put inside of a vector, compare user input to the string array , and then use the index in the array to use the correlating index in the vector to call the function . It seemed like a good idea to me but it failed. And using .push_back
to try to populate the vector in this instance is giving me a
E0304 error: no instance of overloaded function matches the argument list.
Edit: Specifically, I dont know what the syntax: using func= std::function<void()> is actually doing. I just added it to the code to try to get it to work , thats why my understanding is limited in troubleshooting here.
Edit: the E0304 error is fixed by correcting the syntax to reference the function instead of calling it. And i changed functions[i] to functions[i]() to call the functions, although it is still not working.