C++ what to code if i put a class after main() function

前端 未结 5 925
滥情空心
滥情空心 2020-12-30 17:26

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

5条回答
  •  礼貌的吻别
    2020-12-30 17:47

    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.

提交回复
热议问题