gpt4 book ai didi

C++函数问题

转载 作者:行者123 更新时间:2023-11-27 23:30:30 24 4
gpt4 key购买 nike

有没有一种方法可以让函数相互调用,即

void menu()
{
some code here
play();
...
}

int play()
{
...
menu();
...
return 0;
}

最佳答案

在代码文件的顶部添加第二个函数的声明:

int play();

void menu()
{
// some code here
play();
// ...
}

int play()
{
// ...
menu();
// ...
return 0;
}

这叫做 forward declaration , 它通知编译器稍后将声明一个标识符。
它是一种表示函数的方式,以便您可以在提供完整定义之前调用它。

关于C++函数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5770979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com