相关疑难解决方法(0)

递归typedef函数定义:std :: function返回自己的类型

我正在尝试实现一个状态机.状态由类型的函数表示callback_t:callback_t(int&)它返回相同类型的函数.

我不知道如何实现它,因为似乎不允许递归类型函数.

在这里我尝试(作为玩具):

#include <stdio.h>
#include <functional>

typedef std::function< callback_t(int &) > callback_t ;
callback_t f1(int & i)
{
    i++;
    return f1;
}
callback_t f0(int & i)
{
    if(i==0) i++;
    return f1;
}
callback_t start(int & i)
{
    i=0;
    return f0;
}


int main(int argc, char **argv)
{
    callback_t begin = start;
    int i=0;

    while(i<100)
        begin = begin(i);

    printf("hello world\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误:

C:/work/tests/tests/main.cpp:4:41: error: 'callback_t' was not declared in this scope
typedef std::function< callback_t(int …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

17
推荐指数
2
解决办法
2306
查看次数

标签 统计

c++ ×1

c++11 ×1