Obviously, you came from the Java background because you have not yet understood the concept of header files. In Java, the process of defining something is usually in one piece. You simultaneously declare and determine. In C / C ++, this is a two-step process. The declaration says something to the compiler "something exists with this type, but I will tell you later how it is implemented." Defining something gives the compiler the actual part of the implementation. Header files are mainly used for declarations, .cpp files for definitions.
Header files should describe the "API" of the classes, but not their actual code. You can include code in the header called a header. You included everything in CppClass.cpp (not good, nesting of headers should be an exception), and then you declare your class in main.cpp AGAIN, which is a double declaration in C ++. Embedding in the body of the class leads to code duplication every time you use the method (it just sounds crazy. See C ++ faq section onlining for more details .)
Including double declaration in your code gives a compiler error. Leaving a compromise on class code, but gives you a linker error, because now you only have a header type declaration in main.cpp. The compiler does not see code that implements class methods, so errors appear. Unlike Java, the C ++ linker will NOT automatically search for object files that it wants to use. If you use the XYZ class and do not pass the object code for XYZ to it, it just fails.
Please see the Wikipedia header file article and the Include Templates header file (the link is also at the bottom of the Wikipedia article and contains more examples)
In short:
For each class, generate the NewClass.h and NewClass.cpp files.
In the NewClass.h file write:
class NewClass { public: NewClass(); int methodA(); int methodB(); }; <- don't forget the semicolon
In the NewClass.cpp file write:
#include "NewClass.h" NewClass::NewClass() {
In main.cpp write:
#include "NewClass.h" int main() { NewClass nc;
To tie it all together, do
g ++ -o NewClassExe NewClass.cpp main.cpp
(just an example with gcc)