function scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • carmelo

    function scope

    Hi, I wrote a program in c++ and it works. now i want to put all the
    code in 1 file and leave in the main.cpp only a few things. In the
    main I call a function that is in the other file but it doesn't work.
    I tryed to declare the function extern, I tried to include an header
    with the declaration, but the problem is always the same, error C2129:
    static function 'void display(void)' declared but not defined. What
    can I do?
  • jason.cipriani@gmail.com

    #2
    Re: function scope

    "carmelo" <carmelo.abramo @gmail.comwrote in message
    news:cfa08981-
    af30-4018-8d46-242f59fdf253@60 g200...le groups.com...
    Hi, I wrote a program in c++ and it works. now i want to put all the
    code in 1 file and leave in the main.cpp only a few things. In the
    main I call a function that is in the other file but it doesn't work.
    I tryed to declare the function extern, I tried to include an header
    with the declaration, but the problem is always the same, error C2129:
    static function 'void display(void)' declared but not defined. What
    can I do?
    When you declare/define a function as "static", that function is only
    in scope in the file it is declared/defined in (and it is not exported
    from that translation unit either). Don't declare it as static.

    In the file you want to use display() in, declare it simply as:

    void display (void);

    And in the file that defines display, define it just like:

    void display (void) {
    // your code
    }

    The "extern" is implied on function declarations.

    If you start using your display() function in more than one other
    source file, eventually it becomes convenient to just stick the
    declarations in a header file and #include that header in all the
    source files you want to use display() in.

    Jason

    Comment

    • tOmMy

      #3
      Re: function scope

      Maybe,yo u can use "extern" keyword.

      On Feb 27, 5:24 pm, carmelo <carmelo.abr... @gmail.comwrote :
      Hi, I wrote a program in c++ and it works. now i want to put all the
      code in 1 file and leave in the main.cpp only a few things. In the
      main I call a function that is in the other file but it doesn't work.
      I tryed to declare the function extern, I tried to include an header
      with the declaration, but the problem is always the same, error C2129:
      static function 'void display(void)' declared but not defined. What
      can I do?

      Comment

      • carmelo

        #4
        Re: function scope

        On 27 Feb, 10:32, "jason.cipri... @gmail.com"
        <jason.cipri... @gmail.comwrote :
        "carmelo" <carmelo.abr... @gmail.comwrote in message
        >
        news:cfa08981-
        af30-4018-8d46-242f59fdf...@60 g2000hsy.google groups.com...
        >
        Hi, I wrote a program in c++ and it works. now i want to put all the
        code in 1 file and leave in the main.cpp only a few things. In the
        main I call a function that is in the other file but it doesn't work.
        I tryed to declare the function extern, I tried to include an header
        with the declaration, but the problem is always the same, error C2129:
        static function 'void display(void)' declared but not defined. What
        can I do?
        >
        When you declare/define a function as "static", that function is only
        in scope in the file it is declared/defined in (and it is not exported
        from that translation unit either). Don't declare it as static.
        >
        In the file you want to use display() in, declare it simply as:
        >
        void display (void);
        >
        And in the file that defines display, define it just like:
        >
        void display (void) {
        // your code
        >
        }
        >
        The "extern" is implied on function declarations.
        >
        If you start using your display() function in more than one other
        source file, eventually it becomes convenient to just stick the
        declarations in a header file and #include that header in all the
        source files you want to use display() in.
        >
        Jason

        Thank you sooooooooooo much Jason! Now It works!!

        Comment

        • Victor Bazarov

          #5
          Re: function scope

          jason.cipriani@ gmail.com wrote:
          [..]
          In the file you want to use display() in, declare it simply as:
          >
          void display (void);
          >
          And in the file that defines display, define it just like:
          >
          void display (void) {
          // your code
          }
          [..]
          A nit-pick. Consider abandoning the C-induced habit of
          putting the keyword 'void' between parentheses in function
          declarations. It is considered much better to write
          _nothing_ in the argument list if it is to have _nothing_:

          void display();
          ...
          void display() {
          // your code
          }

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          Working...