函数A调用函数B,反之亦然(为什么不起作用)

Func A calls Func B and Vice versa (why does it not work) Experimenting

本文关键字:函数 为什么 不起作用 调用 反之亦然      更新时间:2023-10-16

我正在尝试使用c++,并试图通过这个可爱的网站http://www.learncpp.com/了解它的基础知识

现在我正在尝试下面的代码:

#include <QCoreApplication>
#include <iostream>
using namespace std;

int trollFuncyA(int x){
    x++;
    cout << "we are In A and x = " << x << endl;
    if (x > 20) return 1 ;
    cout << "we are In a1 and x = " << x << endl;
    int trollFuncyB(x);
    cout << "we are In a2 and x = " << x << endl;
    return 0;
}
int trollFuncyB(int x){
    cout << "we are In B9 and x = " << x << endl;
     x++;
     x =  x + 1;
      cout << "we are In B and x = " << x << endl;
     int  trollFuncyA(x);
      cout << "we are In B2 and x = " << x << endl;
      return 0;
}
int main()
{
    int troll = 0;
   trollFuncyA(troll );
    return 0;
}

当我尝试运行它时,我遇到了一些问题:

1.)警告:C4189: 'trollFuncyB':局部变量被初始化但未被引用(我如何解决这个问题或它是不可解决的

2)。我希望int X加起来等于20,但是它只运行了trollFuncyA,直到函数结束,它几乎忽略了trollFuncyB .....不管这个程序有多蠢。有可能跑这趟吗?我只是想在这里做实验,我知道for/while循环。我只是认为这将能够按预期运行

Regards A newby

int trollFuncyB(x);

不调用trollFuncyB。它声明了一个名为trollFuncyBint类型的局部变量,并将其值初始化为x。你会得到这个警告,因为这个局部变量从来没有被使用过。

像这样调用函数:

trollFuncyB(x);

int trollFuncyB(x);等同于int trollFuncyB = x;。这是一个用x初始化的int类型变量的声明。

你应该用:

int trollFuncyB(int); // declaration of the function (may be done outside of the function)
trollFuncyB(x);       // The call