Należy zadeklarować funkcję, przed użyciem/powodować jej. Naprawdę ogłosili wersję z 2 argumentami rectangle
funkcja, ale wydaje się, że zapomniał ogłosić wersję z 1 argumentem, która przyjmuje wersję.
Jak pokazano poniżej, jeśli dodasz ogłoszenie dla wersji z 1 argumentem, to twój program nie działa(skompilowany).
#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;
}
Wyniki pracy programu można zobaczyć tutaj.
Alternatywne rozwiązanie:
Jeśli nie chcesz ogłaszać każdej funkcji oddzielnie, należy po prostu określić ich przed main, a nie ogłaszać ich, jak pokazano poniżej.
#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;
}