【发布时间】:2013-12-03 23:21:19
【问题描述】:
我正在尝试从 Main.cpp 调用我的 Dijkstra() 方法。
#include <iostream>
#include "Alg.h"
int main(int argc, char **argv) {
Alg::dijkstra();
return 1;
}
它在我的头文件的 Alg 类中被删除:
#ifndef Alg_
#define Alg_
#include <iostream>
#include <stack>
using namespace std;
class Alg
{
public:
void tracePath(int x);
void output();
void printArray();
void Initialize();
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
并在我的 Alg.cpp 文件中实现:
void Alg::dijkstra() {
//Create Map
Initialize();
for(int i=0; i<5; i++)
{
current=1;
while(current!=6)
{
//Iterate through and update distances/predecessors
//For loopt 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])
{
//cout<<"Previous distance to "<<j<<" was "<<d[j]<<" from "<<p[j]<<endl;
//cout<<"New smaller distance is "<<distanceArray[current][j]+d[current]<<" from "<<current<<endl;
//Update distance
d[j] = distanceArray[current][j]+d[current];
//Update p
p[j] = current;
}
}
//Go to next row in distanceArray[][]
current++;
} //End while
} //End for
//printArray();
output();
} //End Dijkstras
调用它为 Alg::dijkstra() 得到error: cannot call member function ‘void Alg::dijkstra()’ without object,简单地调用它为 dijkstra() 得到error: ‘dijkstra’ was not declared in this scope。
以前,我在我的 Main.cpp 文件中定义了所有这些方法并且它工作得很好(见这里:http://pastebin.com/67u9hGsL),现在我在这里遗漏了一些东西,因为我已经将它分开了。 dijkstra() 不需要输入,它的所有其他函数都在 header/cpp 文件中。
我怎样才能成功地从 main 调用 dijkstra()?
【问题讨论】:
-
你需要一个该类的实例。但如果你不这样做,或许可以将其设为命名空间,或者将函数设为静态。
标签: c++ compiler-errors dijkstra