大佬教程收集整理的这篇文章主要介绍了C中的静态函数,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个硬编码的Dijkstra算法的例子来找到最短路径.
在阿尔及利亚宣布:
static void dijkstra();
定义在Alg.cpp中:
static void Alg::dijkstra() { //Create Map Initialize(); //Loop to pass through grid multiple times for(int i=0; i<5; i++) { current=1; while(current!=6) { //Iterate through and update distances/predecessors //For loop to go through columns,while current iterates rows for(int j=1; j<7; j++) { //check if Distance from current to this node is less than //distance already stored in d[j] + weight of edge if(distanceArraY[current][j]+d[current]<d[j]) { //update distance d[j] = distanceArraY[current][j]+d[current]; //update predecessor p[j] = current; } } //Go to next row in distanceArraY[][] current++; } //End while } //End for output(); } //End Dijkstras
我想从main中调用我的函数,没有对象.当我在Main.cpp中有所有这些代码时,它工作得很好.将其分解成单独的文件导致@L_833_7@main.cpp:15:错误:’dijkstra’没有在此范围中声明.在搜索SE时遇到的帖子给了我一个印象,为了做到这一点,我需要做方法静态,但我还是没有运气.
我究竟做错了什么?
@H_739_2@main.cpp的:#include <iostream> #include "Alg.h" int main() { dijkstra(); return 0; }
#ifndef Alg_ #define Alg_ #include <iostream> #include <stack> using namespace std; class Alg { public: void tracePath(int X); void output(); void printArray(); void Initialize(); static void dijkstra(); int current,mindex; int distanceArraY[7][7]; //2D array to hold the distances from each point to all others int d[6]; //Single distance array from source to points int p[6]; //Array to keep predecessors int copyD[6]; //Copy of d[] used for sorTing purposes in tracePath() int order[6]; //Contains the order of the nodes path lengths in ascending order }; //End alg class #endif
原始的一体式工作Main.cpp文件:http://pastebin.com/67u9hGsL
以上是大佬教程为你收集整理的C中的静态函数全部内容,希望文章能够帮你解决C中的静态函数所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。