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中有所有这些代码时,它工作得很好.将其分解成单独的文件导致错误Main.cpp:15:错误:’dijkstra’没有在此范围中声明.在搜索SE时遇到的帖子给了我一个印象,为了做到这一点,我需要做方法静态,但我还是没有运气.

我究竟做错了什么?

Main.cpp的:

#include <iostream>
#include "Alg.h"

int main() { 

    dijkstra();
    return 0; 
}

编辑:添加完整的头文件,Alg.h:

#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

解决方法

你应该这样称呼:
Alg::dijkstra();

限制

>不能调用任何其他不是静态的类函数.>无法访问非静态类数据成员.>当构造函数为private / protected时,可以通过new class()实例化一个对象.例如.一个工厂功能.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...