Thread: difference between void and static void function

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    36

    difference between void and static void function

    What is the difference between
    Code:
     void func();
    and
    Code:
    static void func();
    I have a Codeblocks project with two files, main.cpp and main2.cpp.

    main.cpp
    Code:
    #include <iostream>
    #include"main2.cpp"
    using namespace std;
    void fnc_in_main(){
        cout<<"This is fnc_in_main() in main.cpp"<<endl;
    }
    int main()
    {
        cout << "This is main() in main.cpp" << endl;
        fnc_in_main();
        fnc_in_main2();
        return 0;
    }
    main2.cpp
    Code:
    #include <iostream>
    using namespace std;
    static void fnc_in_main2(){
        cout<<"This is fnc_in_main2() in main2.cpp"<<endl;
    }
    in main.cpp, if I don't write
    Code:
    #include"main2.cpp"
    the compiler gives an error saying fnc_in_main2() is not defined

    in main2.cpp, if I do not write
    Code:
    static void fnc_in_main2(){
    the compiler says fnc_in_main2() has been declared multiple times and says the multiple declaration occurred in main2.cpp.

    However, it says here that declaring a function static makes it visible only in the file in which the function was declared, but in my program it looks like making it static actually caused it to be visible in the other file main.cpp.

    Could someone please give me any idea about static functions and working with multiple files? Also, I think static functions inside a class works differently right?
    Again, I did the compiling from codeblocks in this case. If I have to compile multiple files in gcc from the command line, how would I give the command?

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that you attempted to use fnc_in_main2 in main before it was declared. The solution is to forward declare fnc_in_main2. Such a forward declaration might be placed in a header file.

    Your solution with static works because you then end up with two functions named fnc_in_main2: one is visible only in main.cpp and the other is visible only in main2.cpp. As such, you don't break the one definition rule, although the solution sucks because you really only needed one such function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    36
    so what's happening here is, when I use #include, a copy of main2.cpp is made in main.cpp?
    So for this to work I need to put the function in main2.cpp in some header file with header guards?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mahaju
    when I use #include, a copy of main2.cpp is made in main.cpp?
    Yes.

    Quote Originally Posted by mahaju
    So for this to work I need to put the function in main2.cpp in some header file with header guards?
    Yes, a declaration, but not the definition, of the function. The definition of the function can stay in main2.cpp
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    36
    so what does it mean by working with multiple files in large projects, if all the function definitions go into the header files and we just need one cpp file with int main() ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mahaju
    so what does it mean by working with multiple files in large projects, if all the function definitions go into the header files and we just need one cpp file with int main() ?
    That means that you're probably doing it wrong
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    36
    ok I think I understand it a bit
    Thank you very much for your help

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  2. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM
  3. What is this mean? static void *name (void *data)
    By beyonddc in forum C++ Programming
    Replies: 14
    Last Post: 05-05-2004, 03:06 PM
  4. void, void function
    By Furious_George in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2003, 05:04 PM
  5. void non void function
    By modance in forum C Programming
    Replies: 6
    Last Post: 01-28-2003, 08:06 AM