I\'m watching some video tutorials on C++ and i know you must define a function / class before it is used or called. But I like having my main() function at the top, and eve
This is why header files are normally used in C++. When you're saying ClassOne one
, the compiler needs to know what the class looks like to create an object of that type. It's not enough to know that the class exists somewhere (that is enough if all you want is a pointer). So the compiler needs to already have read the definition of the class.
Your class has to be defined before it is first used. Without putting it explicitly before main
, the usual way is to create a header file. So you create ClassOne.h
with the class declaration, and you have #include "ClassOne.h
at the top of your file. In this situation the actual methods of the class would normally be in another source file, ClassOne.cpp
.