【问题标题】:Calling Functions From Main in C++ [duplicate]在 C++ 中从 Main 调用函数 [重复]
【发布时间】: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


【解决方案1】:

你需要创建一个类的实例:

Alg a;
a.dijkstra();

或在类中将dijkstra 设为static 方法:

static void dijkstra();

为了简单起见,我建议您使用第一个选项。

【讨论】:

  • 这会编译,但不会像 dijkstra() 那样提供打印输出。这是在实例上调用它的结果吗?或者无论如何它都应该正常工作? @pippin1289
  • @user3063527 它应该可以正常工作。您的算法问题中还有一些其他错误会阻止输出。
  • @user3063527 这可能是output()的问题
  • 感谢您的帮助。
  • @user3063527 如果这回答了您的问题,请不要忘记接受和/或支持答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 2013-05-28
  • 2021-03-15
相关资源
最近更新 更多