0
#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.

1 Answers1

0

Looks like you've mixed up the function call syntax, and passing an actual (reference to a) function. In your functions.push_back(functionNameHere) remove the inner (), you don't want to call that function, you want to push the function itself to the vector. From the other side, your functions[i] should be functions[i](), cause here you are actually calling the function.

For example, this definitely works:

void testMethod() 
{
    cout << "test method has been called\n";
}

int main()
{
    using func = std::function<void()>;
    std::vector<func> functions;
    functions.push_back(testMethod);
    functions[0]();
}

See it running here - http://cpp.sh/2xvnw

Boris Lipschitz
  • 1,514
  • 8
  • 12
  • Hey, thanks for the tip! that fixed the error. I changed the syntax inside the for loop too to functions[i]() as well, but unfortunately, its still not calling the function..Its a start though. – Freddy Ingle Mar 29 '20 at 04:57
  • @FreddyIngle - I've added a minimal example that definitely works for the reference. – Boris Lipschitz Mar 29 '20 at 05:00
  • Awesome, yeah i see that definitely works that way. How would i get it to call 1 out of 3 functions depending on user input. would i be better off with nested if statements? – Freddy Ingle Mar 29 '20 at 05:08
  • First, remove cin.ignore(), you are ignoring your actual first input instead of reading it. Second, replace the `while` with an `if`, you don't really want to run your function in a while, indefinitely. Other than that, it looks fine, at least for the first sight. Debug if necessary. Here, this is pretty much your code running, with just the changes i've mentioned - https://onlinegdb.com/rJYwe36UU – Boris Lipschitz Mar 29 '20 at 05:17
  • Note that at your original post edit you **did not** add the `()` to the actual function call. – Boris Lipschitz Mar 29 '20 at 05:23
  • You should really try and debug this stuff yourself, though. Debugging a code that seems right, but doesn't behave as expected, is a valuable experience. – Boris Lipschitz Mar 29 '20 at 05:29