Ste deklarovať funkciu pred použitím/zavolať. Ste vyhlásiť 2 argument verzia rectangle
funkciu, ale zdá sa, zabudnúť na vyhlásiť 1 argument, pričom verzia.
Ako je uvedené nižšie, ak sa pridáte vyhlásenie pre 1 argument verzia potom váš program funguje(zhromažďuje).
#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;
}
Výstup programu je možné vidieť tu.
Alternatívne riešenie:
Ak nechcete, aby sa vyhlasujú každú funkciu samostatne, potom by ste mali len definovať ich pred hlavným namiesto vyhlásiť ich, ako je znázornené nižšie.
#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;
}