Home >Backend Development >C++ >Why am I getting the \'HelloWorld() was not declared in this scope\' error in my C code?
Undeclared Function in Scope: Resolving 'HelloWorld' Error
In the C code snippet provided, you encounter a compilation error with the message "HelloWorld() was not declared in this scope." This error occurs when a function call is made without proper declaration or definition in the current scope.
To resolve this issue, you need to either declare the function's prototype using the function's return type and parameters or define the function before you use it. By providing a function declaration, you inform the compiler that the function exists, even if its definition is not yet provided.
In your specific case, you have defined the function HelloWorld() after main(). One solution is to move the function definition before main():
<code class="cpp">#include <iostream> using namespace std; void HelloWorld() { cout << "Hello, World" << endl; } int main() { HelloWorld(); return 0; }</code>
Alternatively, you can declare the function's prototype before main():
<code class="cpp">#include <iostream> using namespace std; void HelloWorld(); int main() { HelloWorld(); return 0; } void HelloWorld() { cout << "Hello, World" << endl; }</code>
By following these steps, you can ensure that the HelloWorld() function is declared or defined before it is called, thereby resolving the compilation error.
The above is the detailed content of Why am I getting the \'HelloWorld() was not declared in this scope\' error in my C code?. For more information, please follow other related articles on the PHP Chinese website!