@LakySimi1, you have problems with your code other than header issues. You have
int main, it's missing the parentheses.
You are using the iostream insertion operator<< to extract from std::cin. And including std::endl when getting input.
Do I need to point out your function defined in your header isn't the function you call in main?
Fix those errors, as well as including all the needed headers so your example code is SSCCE compliant, and try again.
http://sscce.org/
why can't i simply write the full code of my functions in an header files |
You can if you only include your header file(s) in only one .cpp source file.
If you include headers in more than one source file then you have multiple function definition problems.
So really the idea you think of
not separating interface (function declarations) from implementation is gonna bit ya in the butt big time as your code grows.
As Ganado says C++20 and modules somewhat overcomes this problem. If your compiler is capable of C++20 and use modules, great. Not every compiler is fully compliant yet. Visual Studio 2019/2022 are the only two currently.
Here's your code, rewritten for modules (VS 2022):
MyFunction.cppm (module interface file):
1 2 3 4 5 6 7 8 9 10
|
export module MyFunction;
import <iostream>;
export void DoubleTheNumber(int Number)
{
Number *= 2;
std::cout << Number << '\n';
}
| |
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12
|
import <iostream>;
import MyFunction;
int main()
{
int MyNumber;
std::cout << "Insert the number to double.. ";
std::cin >> MyNumber;
DoubleTheNumber(MyNumber);
}
| |
Insert the number to double.. 125
250 |