What is the difference between declaration and definition in C++?
Hi! I recently started learning C++ and I constantly get confused by the terms "declaration" and "definition". They sound similar, but the compiler complains about different things.\n\nHere's an example that confused me. I wrote this code:\n\n```cpp\n#include \n\nint x; // Is this a declaration or a definition?\nextern int y; // And this?\n\nvoid foo(); // And this?\n\nint main() {\n std::cout << x;\n return 0;\n}\n\nvoid foo() {\n // some implementation\n}\n```\n\nIf I remove `void foo() { ... }` and leave only `void foo();`, the compiler will give a linker error. But if I write `int x;` without `extern`, everything works. Why is that? In what cases is `int x;` a declaration, and in what cases is it a definition? And what is ODR (One Definition Rule)? Please explain in simple terms, with ex
No answers yet.
Log in to leave an answer