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 algorithm for finding the shortest path.
Declared in Alg.h:
static void dijkstra();
Defined in 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
I want to call my function from main without an object. When I had all of this code in Main.cpp, it worked perfectly. Splitting it up into separate files caused the error Main.cpp:15: error: ‘dijkstra’ was not declared in this scope
.The posts I came across when searching SE gave me me the impression that to do this, I needed to make that method static, yet I still have no luck.
What am I doing wrong?
Main.cpp:
#include <iostream>
#include "Alg.h"
int main() {
dijkstra();
return 0;
}
Edit: Added full header file, 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
Original all-in-one working Main.cpp file: http://pastebin.com/67u9hGsL
Are you sure the function is supposed to be static?
It looks as if you want just a function? in your header file:
in your cpp file
in your main file:
if you really want a static function you have to put it into a nested class:
the implementation somewhere in a cpp file
and then in your cpp file where the main resides
You can just use a namespace instead of having a class with all static members.
Alg.h:
and in Alg.cpp
in main.cpp
You should call it this way:
Limitations
new class()
when constructor is private/protected. E.g. a factory function.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.
In your header file
Alg.h
:The include guards are necessary if you plan to include the header in more than one of your cpp files. It seems you would like to put the function in a namespace
Alg
, right?In Alg.cpp:
Then, in main.cpp you call it with full namespace qualification:
If you just want to distribute your code over several files, I don't see why the function should be declared
static
.Now that we have the complete declaration of your class
Arg
, it feels like the singleton design pattern could be useful:http://en.wikipedia.org/wiki/Singleton_pattern