C ++中的静态函数 [英] Static Functions in C++
问题描述
我在这里阅读了一些关于静态函数的文章,但仍然遇到了实现麻烦。
我正在编写一个Dijkstra算法的硬编码示例寻找最短路径。
在Alg.h中声明:
$ b
static void dijkstra() ;
在Alg.cpp中定义:
static void Alg :: dijkstra(){
//创建映射
Initialize();
//循环多次通过网格
for(int i = 0; i <5; i ++)
{
current = 1;
while(current!= 6)
{
//遍历并更新距离/前置元素
// For循环遍历列,而当前迭代行
for(int j = 1; j <7; j ++)
{
//检查当前到该节点的距离是否小于
//距离已存储在d [j] + (distanceArray [current] [j] + d [current]< d [j])
{
//更新距离
d [ j] = distanceArray [current] [j] + d [current];
//更新前任
p [j] = current;
}
}
//转到distanceArray [] []中的下一行
current ++;
} // End while
} // End for
output();
} //结束Dijkstras
我想从main 调用我的函数,而没有一个对象。当我在Main.cpp中拥有所有这些代码时,它完美运行。将它拆分成单独的文件导致错误 Main.cpp:15:错误:'dijkstra'未在此范围内声明
。搜索SE时遇到的帖子给了我我觉得要做到这一点,我需要使这种方法是静态的,但我仍然没有运气。
我做错了什么?
Main.cpp:
#include< iostream>
#includeAlg.h
int main(){
dijkstra();
返回0;
}
编辑:添加完整的头文件Alg.h:
#ifndef Alg_
#define Alg_
#include< iostream>
#include< stack>
使用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数组保存每个点到所有其他点的距离
int d [6]; //从源到点的单个距离数组
int p [6]; //数组保存前辈
int copyD [6]; //在tracePath()中用于排序的d []副本
int order [6]; //以升序包含节点路径长度的顺序
}; //结束alg类
$ b #endif
原始多合一运行Main.cpp文件: http://pastebin.com/67u9hGsL
您应该这样调用它:
Alg :: dijkstra();
限制
- 无法调用任何其他不是静态的类函数。
- 无法访问非静态类数据成员。
- 当构造函数是private / protected时,可以通过
new class()
实例化一个对象。例如。一个工厂功能。
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
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.
这篇关于C ++中的静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!