Placing C++ class in another file: compilation error
I am learning about C++ classes and I realized that if I tried to move my clas开发者_JAVA百科s declaration and definition of class Date
from main.cpp
to another c++ file, say test.cpp
and compiled the two files I got an error saying Date was not declared. Why is that?
This is why you have header files. You need a header file test.h
that contains only the class definition (that is mostly function declarations) and test.cpp
that contains the actual function definitions (the code).
In main.cpp
you'll have to #include "test.h"
.
Well, you would have to declare your class in test.h and include it in main.cpp so that the compiler knows where to look for it and you can then define your class in test.cpp
精彩评论