Static Functions in C++

前端 未结 8 1261
野趣味
野趣味 2021-01-17 08:09

I\'ve read a few posts on here about static functions, but still am running into trouble with implementation.

I\'m writing a hardcoded example of Dijkstra\'s algorit

相关标签:
8条回答
  • 2021-01-17 08:36

    You can just use a namespace instead of having a class with all static members.

    Alg.h:

    namespace Alg
    {
       void dijkstra();
    }
    

    and in Alg.cpp

    namespace Alg
    {
       void dijkstra()
       {
         // ... your code
       }
    }
    

    in main.cpp

    #include "Alg.h"
    
    int argc, char **argv)
    {
      Alg::dijkstra();
    
      return 1;
    }
    
    0 讨论(0)
  • 2021-01-17 08:40

    Are you sure the function is supposed to be static?

    It looks as if you want just a function? in your header file:

    #ifndef DIJKSTRA_H
    #define DIJKSTRA_H
    void dijkstra(); 
    #endif
    

    in your cpp file

    void dijkstra() {
       /* do something */
    }
    

    in your main file:

    #include "yourcppfile.h"
    
    int main(int argc, char **argv) {
        dijkstra();
    }
    

    if you really want a static function you have to put it into a nested class:

    class Alg {
      public:
        static void dijkstra();
      /* some other class related stuff */
    }
    

    the implementation somewhere in a cpp file

    void Alg::dijkstra() {
      /* your code here */
    }
    

    and then in your cpp file where the main resides

    #include "your header file.h"
    
    int main(int argc, char **argv) {
      Alg::dijkstra();
    }
    
    0 讨论(0)
  • 2021-01-17 08:44

    The key here is the ‘dijkstra’ was not declared in this scope error.

    Take your all-in-one source file and remove the main function. Make a new source file with this in it:

    void dijkstra();
    void output();
    
    int main(int argc, char *argv[]) {
        dijkstra();
        output();
        return 0;
    }
    

    The all-in-one cpp without a main plus this file above should compile together and give you the same result as before with one source, as it does for me. You will get a duplicate symbol _main error if you forgot to remove the main from the algorithm file.

    No static needed.


    My answer here fails to touch on good practices on header files, that is, you would want to include those function declarations in a .h file. It solves the compile-time error though.

    You may want to find a good book to help you through some of the machinery of C++, where program context (in a linguistic sense) can change the meaning of keywords. This can be bewildering, and it proves to be exactly that for a language with as much colorful history as C++. Take a look here for book suggestions.

    0 讨论(0)
  • 2021-01-17 08:45

    You should call it this way:

    Alg::dijkstra();
    

    Limitations

    • Can't call any other class functions that are not static.
    • Can't access non static class data members.
    • Can instantiate an object via new class() when constructor is private/protected. E.g. a factory function.
    0 讨论(0)
  • 2021-01-17 08:49

    If I remember right any 'static' function is limited to the module in which it is implemented. So, 'static' prevents using the function in another module.

    0 讨论(0)
  • 2021-01-17 08:49

    You are confusing the 'static' keyword for local functions, with the 'static' keyword used in a class to make a function a class function and not an object function.

    Remove static the first line of Alg.cpp and in the header file. This will allow Alg.o to contain global symbols that main can refer to and the linker can link.

    You still need to call Alg::dijkstra() as was stated by @egur.

    After this you may still get errors. The way you are using Alg:: is more like a namespace than a 'class' definition.

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