The problem:
I hate writing headers or declaring my functions if my computer can do it faster.
And it is the case.
What I tried to do:
I tried to declare a function foo
after the main
function.
But the compiler returns an error:error: ‘foo’ was not declared in this scope
The code:
#include <iostream>
//no function declaration is allowed, please.
//no other header is allowed, please.
void main() {
foo();
}
void foo() {
std::cout << "The compiler is smart now!" << std::endl;
}
I accept to change the compiler if gcc/g++ is not able to compile this c++ code.
Any response will be greatly thanked.
This is not possible with freestanding functions as the languages require at least a function prototype to call it. Having said that, in C++ you can use classes to achieve a similar result:
class Main {
public:
static int main() {
foo();
return 0;
}
static void foo() { }
};
int main() {
return Main::main();
}
Whether or not this is a good practice is a different discussion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With