Reputation: 13
I'm writing my first C++ application. But I get syntax error.
#include <iostream>
using namespace std;
int main() {
int result = get_num();
cout << "Result is " << result << endl;
system("pause");
return 0;
}
int get_num(void) {
return 1;
}
And compiler said me:
main.cpp(10): error C3861: 'get_num': identifier not found
Upvotes: 1
Views: 134
Reputation: 160
You need to declare or define the function before using it in the main. To declare just add
int get_num(void);
at the beginning of your code. If not define the entire function before main like so- Try-
#include <iostream>
using namespace std;
int get_num(void) {
return 1;
}
int main() {
int result = get_num();
cout << "Result is " << result << endl;
system("pause");
return 0;
}
Upvotes: 1
Reputation: 399
In C++ you need to declare all variables/functions that you want to use before using them. You're using getnum in main but you haven't declared it in the function. Writing int get_num(); outside main will declare this at a global scope. i.e. any function in that file would be able to use it. declaring get_num(); inside a function will enable you to use this function only inside that particular function.
Upvotes: 1
Reputation: 23664
Two options:
1) declare a prototype of get_num
before main
:
int get_num(void);
int main() {
}
2) move your definition of get_num
before main
.
Upvotes: 4
Reputation: 5525
Write int get_num(void);
above the main()
function.
C++ requires variables and functions to be declared above the current scope.
Upvotes: 2
Reputation: 158619
One solution would be to add a forward declaration before main
like so:
int get_num(void) ;
the other solution would be to put the definition of get_num
before main
and then you would not need a forward declaration.
Upvotes: 2