#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]();
}
}
Я попытался найти ответ и узнал об использовании std::function
, но я не до конца понимаю, что он делает. Довольно сложно найти ответ на этот вопрос в Интернете. По сути, я хочу, чтобы три функции были помещены в вектор, сравнили пользовательский ввод с массивом строк, а затем использовали индекс в массиве, чтобы использовать коррелирующий индекс в векторе для вызова функции. Это показалось мне хорошей идеей, но она провалилась. И использование .push_back
для заполнения вектора в этом случае дает мне
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.