This is a place in which C++ has a strange rule. Before being able to compile a call to a function the compiler must know the function name, return value and all parameters.
This can be done by adding a "prototype". In your case this simply means adding before main
the following line:
int writeFile();
this tells the compiler that there exist a function named writeFile
that will be defined somewhere, that returns an int
and that accepts no parameters.
Alternatively you can define first the function writeFile
and then main
because in this case when the compiler gets to main
already knows your function.
Note that this requirement of knowing in advance the functions being called is not always applied. For example for class members defined inline it's not required...
struct Foo {
void bar() {
if (baz() != 99) {
std::cout << "Hey!";
}
}
int baz() {
return 42;
}
};
In this case the compiler has no problem analyzing the definition of bar
even if it depends on a function baz
that is declared later in the source code.