【问题标题】:How to solve this indirect recursion error? [duplicate]如何解决这个间接递归错误? [复制]
【发布时间】:2021-06-25 04:04:45
【问题描述】:
#include <iostream>
#include <stdio.h>
using namespace std;

void funB(int n){
    if (n>1){
    cout<<n<<" ";
    funA(n/2);
}
}
void funA(int m)
{
    if (m>0){
        cout<<m<<" ";
        funB(m-1);
        }
}

int main()
{
    funA(20);
    return 0;
}

对于此代码,我收到以下错误:

prog.cpp: In function ‘void funB(int)’:
prog.cpp:8:13: error: ‘funA’ was not declared in this scope
     funA(n/2);
             ^

对于这个简单的间接递归示例,可能的错误是什么? 我什至尝试改变函数的顺序。

【问题讨论】:

    标签: recursion data-structures dsa mutual-recursion recursion-schemes


    【解决方案1】:

    试试这个。我所做的是,我刚刚在定义两个函数之前声明了 funA,以便编译器知道存在一个正在引用的函数“funA”。这称为前向声明。

    #include <iostream>
    #include <stdio.h>
    using namespace std;
    void funA(int);
    void funB(int n){
        if (n>1){
        cout<<n<<" ";
        funA(n/2);
    }
    }
    void funA(int m)
    {
        if (m>0){
            cout<<m<<" ";
            funB(m-1);
            }
    }
    
    int main()
    {
        funA(20);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 2022-07-01
      相关资源
      最近更新 更多