Why can't a function go after Main

前端 未结 8 1900
一向
一向 2020-12-09 12:37

Why can\'t I put a function after main, visual studio cannot build the program. Is this a C++ quirk or a Visual Studio quirk?

eg.

int main()
{
   myF         


        
8条回答
  •  没有蜡笔的小新
    2020-12-09 12:55

    You cannot use a name/symbol which is not yet declared. That is the whole reason.

    It is like this:

    i = 10;  //i not yet declared
    
    int i;
    

    That is wrong too, exactly for the same reason. The compiler doesn't know what i is – it doesn't really care what it will be.

    Just like you write this (which also makes sense to you as well as the compiler):

    int i;  //declaration (and definition too!)
    
    i = 10;  //use
    

    you've to write this:

    void myFunction(); //declaration!
    
    int main()
    {
       myFunction() //use
    }
    
    void myFunction(){}  //definition
    

    Hope that helps.

提交回复
热议问题