Thread: "function definition is not allowed here" error?

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    2

    "function definition is not allowed here" error?

    Hello! I'm a senior in high school and I've been trying to learn C in order to get into serious coding (I've been using basic stuff like Scratch for the past 6 years). I've made attempts to learn it before but I've never really committed to it until the beginning of this school year now that I'm taking a computer science class. I'm picking up on it pretty quickly, as like I said I have fairly extensive experience with kids' languages like Scratch so I do have a general sense of how programming works.

    Anyways, I've been working hard lately on my first actual "project" in a sense- it's nothing special, but I've essentially created an ASCII version of the board game "Battleship" with user inputs and whatnot. It's not perfectly coded- there are some flaws with it as well as a bunch of issues with variables that I had to just work my way around since I couldn't figure out why they were happening, and I'm sure I could have written it more efficiently- but I've gotten it to function using OnlineGDB, an in-browser compiler which I've been mainly writing the code in. I used stdio.h, stdlib.h, and initially conio.h before I switched it out with scurses.h so it'd work on my MacBook.

    However, the issues seem to be popping up whenever I try to run the code in any other compilers (by which I mean the 2 others that I attempted lol). I installed Xcode earlier today on my MacBook and tried running the .c file through the terminal, and was greeted with this error message:

    error: function definition is not allowed here
    {

    and the same thing happens whenever I try to run the file in repl.it, another online compiler. Furthermore, in both cases the error happens 10 times, each appearing wherever some function or another is supposed to be defined, and is linked to the following lines in main.c:

    • 85.3
    • 151.3
    • 173.3
    • 198.3
    • 283.3
    • 313.3
    • 364.3
    • 456.3
    • 507.3
    • 540.3


    Beyond this there are several assorted notes and warnings relating to undeclared functions and whatnot, but I’ve generally been under the impression that these are all connected to the above 10 errors.

    I honestly have no clue what the issue here may be. From what I’ve looked into, it appears that this error shows up when an opening bracket appears without a companion closing one or vice versa, but I used the cmd + f thing to check and I believe there were perfectly equal numbers of opening and closing brackets (I think 134 each, to be exact). Furthermore, even if the brackets were unbalanced, that still wouldn’t explain why it works whenever I run it through onlineGDB, but not through Terminal or repl.it. Does anyone here have any clue what the issue(s) may be?

    I've attached the .c file for the project below, and here is the link again to the repl.it upload of the file. Please let me know if you need a tl;dr, or if there are any details that you need clarification on. Thanks!

    ~Tristan

    Battleship (ncurses).c



  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How did you manage to write nearly 1000 lines of code, only to discover that it doesn't work.

    You have badly mis-matched braces in your code, and this will cause the error you see.

    You need to go through the code, indent it properly, and figure out where the braces are supposed to go.

    Here is where it starts to go pear shaped.
    Code:
      int otherplayer;
    
      // No, this is inside main and it shouldn't be
      int coordyinput ()
      {
        scanf ("%c%i", &yentered_char, &xentered);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    2
    Quote Originally Posted by Salem View Post
    How did you manage to write nearly 1000 lines of code, only to discover that it doesn't work.

    You have badly mis-matched braces in your code, and this will cause the error you see.

    You need to go through the code, indent it properly, and figure out where the braces are supposed to go.

    Here is where it starts to go pear shaped.
    Code:
      int otherplayer;
    
      // No, this is inside main and it shouldn't be
      int coordyinput ()
      {
        scanf ("%c%i", &yentered_char, &xentered);
    Like I said, the code has been working consistently fine whenever I've run it in the first online compiler I linked. Copy it onto there and see for yourself (it's this one)– upon quickly looking further into this it seems that certain compilers such as GCC contain extensions that allow for nested functions. That's a bit annoying in retrospect...

    Either way though, thanks a lot for the insight. I never realized that you couldn't define a function within another function but thinking about it I guess it's a bit of a no-brainer. I'll try and move all of the function declarations (as well as most of the variables I suppose) outside of main() and hopefully I'll be able to fix all of the issues that have popped up as a result of this. Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-22-2015, 04:43 PM
  2. Replies: 4
    Last Post: 05-06-2015, 10:23 PM
  3. Replies: 2
    Last Post: 01-25-2010, 01:55 AM
  4. Getting a "multiple definition of <classname>" error.
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2007, 07:43 PM
  5. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM

Tags for this Thread