你已经宣布的功能在你之前使用/叫它。 你有没有宣布的2个参数的版本 rectangle
功能,但你似乎忘记了声明1中的参考虑的版本。
如下图所示,如果你加入该《宣言》为1参数的版本,然后你的节目工作(汇编).
#include <iostream>
using namespace std;
//declare the function before main
int rectangle(int p, int l);
int rectangle(int p);//ADDED THIS DECLARATION
int main() {
cout << rectangle(3);
return 0;
}
//define the functions after main
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
该程序的输出可以看到 在这里.
备选方案:
如果你不想要宣布每一个功能分开,那么你应该将它们定义之前,主要的而不是宣布它们,如下所示。
#include <iostream>
using namespace std;
//define the functions before main. This way there is no need to write a separate function declaration because all definition are declarations
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
int main() {
cout << rectangle(3);
return 0;
}