Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 添加函数数组并随机选择其中一个_C++_Arrays_Xcode_Function_Compiler Errors - Fatal编程技术网

C++ 添加函数数组并随机选择其中一个

C++ 添加函数数组并随机选择其中一个,c++,arrays,xcode,function,compiler-errors,C++,Arrays,Xcode,Function,Compiler Errors,我一直在寻找一种创建函数数组的方法。我使用Xcode,每次尝试时,它都会提示我警告 应用于函数样式转换或类型构造 我不太确定该怎么办。下面是一个代码示例 //possibleResponses int Responses[]{ //this is where the error is. int possibleResponse1; { cout << "Continue...\n"; getline (cin, inputc); } int possibleRe

我一直在寻找一种创建函数数组的方法。我使用Xcode,每次尝试时,它都会提示我警告

应用于函数样式转换或类型构造

我不太确定该怎么办。下面是一个代码示例

//possibleResponses

int Responses[]{ //this is where the error is.

int possibleResponse1;
{
    cout << "Continue...\n";
    getline (cin, inputc);
}

int possibleResponse2 (0);
{
    cout << "Text" << inputb << ".\n";
    getline (cin, inputd);
}

为了澄清,我的程序可以给出20种不同的可能响应。我想把它们放在一个数组中,然后随机化输出。如果有人可以帮助我,如果可能的话,这将是非常好的,添加示例代码,我学习更好的方式,提前谢谢你

我不确定您对发布的代码做了什么,但我确定它不会编译。我将尝试演示如何添加函数数组并随机选择一个

int function1() { ... } // implementation is irrelevant
int function2(int x) { ... }
int function3(char x) { ... }
客户端代码:

#include <functional>
#include <vector>

std::vector<std::function<int(void)>> functions; // all functions placed
                                                 // in here take no parameters

functions.push_back(function1);
functions.push_back( []() { return function2(190); } ); // adaptor functor, calling
                                                        // function2 with parameters
                                                        // the adaptor matches the
                                                        // signature of
                                                        // functions::value_type
functions.push_back( []() { return function3('s'); } ); // same as above
#include <random>

std::random_device rd;
static std::mt19937 gen(rd()); // mersene twister
std::uniform_int_distribution<> d(0, functions.size());
auto itr = std::begin(functions);
std::advance(itr, dis(gen)); // advance iterator by random number of positions

(*itr)(); // call the functor
#include <random>

std::random_device rd;
static std::mt19937 gen(rd()); // mersene twister
std::uniform_int_distribution<> d(0, functions.size());
auto index = dis(gen)); // get index in range [0, functions.size())

functions[index]();
namespace responses {
    int function1() { ... } // implementation is irrelevant
    int function2(int x) { ... }
    int function3(char x) { ... }
}