0

I'm a noob to C++ and mainly used scripting languages like python to get by. Compiling languages are new to me and I am having a hard time understanding #include, headers, and so on.

I was recently trying to write a program with a class, called Point.cpp. So, I tried including this class into my main file by #include "Point.cpp". This prompted an error saying that the constructors were defined multiple times. A StackExchange post addressed a similar problem and advised to include the header instead. This time the code compiled without a problem and worked as expected.

It seems strange that this would work because I have already included "Point.hh" into Point.cpp, so it seemed logical (transitive) to include Point.cpp into main.cpp.

Can someone weigh in on this? I would like to understand this better. Thanks.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Astaboom
  • 317
  • 2
  • 7
  • 1
    The header file doesn't contain the _definition_ of the class, but it's _declaration_. The latter contains all the information compiler needs, that the class can be used properly in any translation unit. – πάντα ῥεῖ May 21 '16 at 19:59
  • 1
    Never include .cpp file (If you want to anyway, rename it as a header). Your IDE usualy compiles *.cpp and then links those together. So, if you include it as cpp, it will be in your program executable twice. – krOoze May 21 '16 at 20:57
  • @krOoze _"If you want to anyway, rename it as a header"_ That's particularly answered and relevant here: [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – πάντα ῥεῖ May 21 '16 at 21:31

0 Answers0