我有这个代码.如何在不创建错误的情况下执行此操作?
int function1() {
if (somethingtrue) {
function2();
}
}
int function2() {
//do stuff
function1();
}
Run Code Online (Sandbox Code Playgroud)
这是前瞻性声明的情况.前向声明告诉编译器该名称将存在,它的类型是什么,并允许您在定义它之前在有限的上下文中使用它.
int function2(); // this lets the compiler know that that function is going to exist
int function1() {
if (somethingtrue) {
function2(); // now the compiler know what this is
}
}
int function2() { // this tells the compiler what it has to do now when it runs function2()
//do stuff
function1();
}
Run Code Online (Sandbox Code Playgroud)
小智 2
只需将其放在您的功能之上:
int function1();
int function2();
Run Code Online (Sandbox Code Playgroud)
但不要创建无限循环!通过这两行,您可以告诉编译器function1和function2将在将来定义。在较大的项目中,您将使用头文件。您可以执行相同的操作,但可以使用多个文件中的函数。
并且不要忘记 return 语句。我认为您的代码示例只是演示,但我只想提及它。
在 C++ 中,必须将声明和定义分开。在这里阅读更多相关信息:/sf/answers/98744271/
| 归档时间: |
|
| 查看次数: |
734 次 |
| 最近记录: |