Why are #ifndef and #define needed in C++ header files?
Hello everyone! I just started learning C++ and am writing my first multi-file program. I have three files: `main.cpp`, `functions.h`, and `functions.cpp`.\n\nHere is the content of `functions.h`:\n\n```cpp\n// functions.h\nvoid printHello();\nint add(int a, int b);\n```\n\nIn `functions.cpp`, I include this header file and define the functions:\n\n```cpp\n// functions.cpp\n#include "functions.h"\n#include \n\nvoid printHello() {\n std::cout << "Hello, world!" << std::endl;\n}\n\nint add(int a, int b) {\n return a + b;\n}\n```\n\nAnd in `main.cpp`, I also include `functions.h`:\n\n```cpp\n// main.cpp\n#include "functions.h"\n#include \n\nint main() {\n printHello();\n std::cout << add(3, 4) << std::endl;\n return 0;\n}\n```\n\nEverything works. But I noticed
No answers yet.
Log in to leave an answer