函数A调用函数B,反之亦然(为什么不起作用)
Func A calls Func B and Vice versa (why does it not work) Experimenting
我正在尝试使用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
。它声明了一个名为trollFuncyB
的int
类型的局部变量,并将其值初始化为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
相关文章:
- C++:调用没有匹配函数:为什么需要一个空的构造函数?
- 我是C++新手,我想知道,为什么在c ++中使用纯虚函数?为什么要使用纯虚函数?
- 无法重新声明构造函数.为什么?
- 通过指针调用模板类成员函数 [为什么这是有效的 c++]?
- 将指针传递给函数 - 为什么我无法打印地址?
- 我打算调用initializer_list构造函数,如果存在,则事先调用复制构造函数:为什么?
- C ++中的函数为什么我的编译器无法识别错误()
- 成员函数与友元函数:为什么是一个而不是另一个?
- 可怕的元函数:为什么它们更快?
- 调用自动构造函数:为什么我的类型不完整
- cpp 没有匹配的函数调用来调用构造函数.为什么
- POW函数为什么返回意外值
- 模板中的友元函数(为什么这在Visual Studio中失败,而不是GCC和Clang)
- 显式函数为什么
- C++构造函数初始化列表调用默认构造函数.为什么
- 未调用构造函数?为什么
- C++:自动向量重新分配调用复制构造函数?为什么
- C++ / 多态性 / 虚函数 / 为什么我的子类的函数没有被调用?
- Linux vs Windows std::map 赋值构造函数(为什么会有这样的区别?)
- 错误:使用已删除的函数.为什么?