note: previous implicit declaration of ‘point_forward’ was here

后端 未结 1 1708
说谎
说谎 2020-12-06 06:33

I can\'t seem to get this recursive function to compile properly, and I\'m not sure why. The code is as follows:

void point_forward (mem_ptr m) {
  mem_ptr t         


        
相关标签:
1条回答
  • 2020-12-06 07:14

    The key is in this:

    previous implicit declaration of ‘point_forward’ was here

    On line 96 you have:

    point_forward(m); // where m is a mem_ptr;
    

    Since the compiler hasn't yet seen a function declaration for point_forward(m), it "implicitly defines" (ie, assumes) a function that returns an int:

    int point_forward(mem_ptr m);
    

    This conflicts with the definition later:

    void point_forward (mem_ptr m) {
    

    To fix this, you can either:

    1. Put an explicit declaration somewhere before line 96: void point_forward(mem_ptr m); This will tell the compiler how to treat point_forward() when it sees it on line 96, even though it hasn't yet seen the function implementation.

    2. Or, define the whole function above line 96 (move the function definition from line 134 onwards to above line 96).

    Here is a little bit more about declaring functions.

    Generally, for style, I would either:

    • If you don't want to use point_forward() in any other C files, define it in full:

      static void point_forward(mem_ptr m) { ..function body goes here.. }

      at the top of the source file.

    • If you want to use point_forward() in other C files, put a forward declaration:

      void point_forward(mem_ptr m);
      

      in a header file for other files to include.

    0 讨论(0)
提交回复
热议问题