c++ - Why classes in different files do not find each other without a header? -
please have @ following code
main.cpp
#include <iostream> using namespace std; int main() { class1 c; } class1.cpp
#include <iostream> using namespace std; class class1 { public: void click1() { cout << "click 1" << endl; } }; class2.cpp
#include <iostream> using namespace std; class class2 { public: void click2() { cout << "click 2" << endl; } }; if add header files above classes, work. why c++ not understand classes in different files without header file?
in c++ source file called translation unit. each translation unit separate each other, , don't know each others existence. have explicitly tell compiler things translation unit should know about.
this done declaring things. , instead of having same declaration in many files , places, put them in single header file source files includes.
Comments
Post a Comment