新手求函数调用的一点小问题
这其实是练习题,可是做不出来。
要使用3个用户定义的函数(包括main()函数),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
其中一个函数要调用两次,该函数生成前两行;另一个函数也被调用两次,生成其余的输出。
#include<iostream>
using namespace std;
void showT()
{
cout<<"Three blind mice"<<endl;
void showT();
}
void showS()
{
cout<<"See how they run"<<endl;
void showS();
}
int main()
{
void showT();
void showS();
return 0;
}
程序生成的时候没有输出,只有“请按任意键继续”,我用的是VC2010。
[解决办法]
void showT();
void showS(); //这是函数的声明 不是调用
showT();
showS();
[解决办法]
void showT();
代表函数声明,没有执行。
你把void去掉就会执行了。
[解决办法]
- C/C++ code
#include<iostream>using namespace std;void showT(){ cout<<"Three blind mice"<<endl;}void showS(){ cout<<"See how they run"<<endl;}int main(){ showT(); showT(); showS(); showS(); return 0;}
[解决办法]
不止如此,去掉void之后你的函数会形成无限递归。
- C/C++ code
#include<iostream>using namespace std;const int num = 2; //函数调用次数void showT(){ cout<<"Three blind mice"<<endl;}void showS(){ cout<<"See how they run"<<endl;}int main(){ for(int i = 0; i < num; ++i) { showT(); } for(int j = 0; j < num; ++j) { showS(); } return 0;}