Static Functions in C++

前端 未结 8 1299
野趣味
野趣味 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: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.

提交回复
热议问题