function scope

Discussion in 'C++' started by carmelo, Feb 27, 2008.

  1. carmelo

    carmelo Guest

    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?
    carmelo, Feb 27, 2008
    #1
    1. Advertising

  2. carmelo

    Guest

    "carmelo" <> wrote in message
    news:cfa08981-
    ...
    > 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
    , Feb 27, 2008
    #2
    1. Advertising

  3. carmelo

    tOmMy Guest

    Maybe,you can use "extern" keyword.

    On Feb 27, 5:24 pm, carmelo <> wrote:
    > 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?
    tOmMy, Feb 27, 2008
    #3
  4. carmelo

    carmelo Guest

    On 27 Feb, 10:32, ""
    <> wrote:
    > "carmelo" <> wrote in message
    >
    > news:cfa08981-
    > ...
    >
    > > 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!!
    carmelo, Feb 27, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    931
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    652
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    459
  4. Xah Lee
    Replies:
    0
    Views:
    2,223
    Xah Lee
    Feb 26, 2009
  5. Andrew Falanga
    Replies:
    2
    Views:
    194
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page