Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中的静态函数+;_C++_Function_Static - Fatal编程技术网

C++ C+中的静态函数+;

C++ C+中的静态函数+;,c++,function,static,C++,Function,Static,我在这里读了一些关于静态函数的帖子,但在实现方面仍然遇到了麻烦 我正在写一个Dijkstra的最短路径算法的硬编码示例 在Alg.h中声明: 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++) { cur

我在这里读了一些关于静态函数的帖子,但在实现方面仍然遇到了麻烦

我正在写一个Dijkstra的最短路径算法的硬编码示例

在Alg.h中声明:

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
#include "Alg.h"

void Alg::dijkstra() { /* your implementation here */ }
编辑:添加了完整的头文件,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
#ifndef Alg_
#定义Alg_
#包括
#包括
使用名称空间std;
类Alg
{
公众:
无效跟踪路径(int x);
无效输出();
void printary();
void初始化();
静态空洞dijkstra();
int电流,mindex;
int distanceArray[7][7];//用于保存每个点到所有其他点的距离的2D数组
int d[6];//从源到点的单距离数组
int p[6];//保留前导的数组
int copyD[6];//在tracePath()中用于排序的d[]的副本
int order[6];//包含节点路径长度的升序
}; //结束alg类
#恩迪夫

原始的一体式工作Main.cpp文件:

您应该这样称呼它:

Alg::dijkstra();
限制

  • 无法调用任何其他非静态的类函数
  • 无法访问非静态类数据成员
  • 当构造函数是私有/受保护的时,可以通过
    newclass()
    实例化对象。例如,工厂功能

如果我没记错的话,任何“静态”功能都仅限于实现它的模块。因此,“static”会阻止在另一个模块中使用该函数。

您将本地函数的“static”关键字与类中使用的“static”关键字混淆,使函数成为类函数而不是对象函数

删除Alg.cpp的第一行和头文件中的
static
。这将允许Alg.o包含
main
可以引用且链接器可以链接的全局符号

您仍然需要调用
Alg::dijkstra()
,正如@egur所述


在此之后,您仍然可能会出现错误。您使用Alg::的方式更像是一个
名称空间,而不是一个“类”定义。

您确定函数应该是静态的吗

看起来你只想要一个函数? 在头文件中:

#ifndef DIJKSTRA_H
#define DIJKSTRA_H
void dijkstra(); 
#endif
在您的cpp文件中

void dijkstra() {
   /* do something */
}
在主文件中:

#include "yourcppfile.h"

int main(int argc, char **argv) {
    dijkstra();
}
如果确实需要静态函数,则必须将其放入嵌套类中:

class Alg {
  public:
    static void dijkstra();
  /* some other class related stuff */
}
cpp文件中某处的实现

void Alg::dijkstra() {
  /* your code here */
}
然后在主文件所在的cpp文件中

#include "your header file.h"

int main(int argc, char **argv) {
  Alg::dijkstra();
}

您可以只使用名称空间,而不使用包含所有静态成员的类

Alg.h:

namespace Alg
{
   void dijkstra();
}
在Alg.cpp中

namespace Alg
{
   void dijkstra()
   {
     // ... your code
   }
}
in main.cpp

#include "Alg.h"

int argc, char **argv)
{
  Alg::dijkstra();

  return 1;
}

在头文件
Alg.h
中:

#ifndef __ALG_H__
#define __ALG_H__

namespace Alg {

    void dijkstra();

}

#endif
如果计划在多个cpp文件中包含标头,则需要包含防护。似乎您希望将函数放在名称空间
Alg
,对吗

在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
#include "Alg.h"

void Alg::dijkstra() { /* your implementation here */ }
然后,在main.cpp中,您可以使用完整的命名空间限定来调用它:

#include "Alg.h"

int main() {

    Alg::dijkstra();

}

如果您只想将代码分发到多个文件中,我不明白为什么要声明函数
static

既然我们已经完成了类
Arg
的完整声明,那么感觉单例设计模式可能很有用:


这里的关键是
'dijkstra'未在此范围内声明
错误

获取您的多功能源文件并删除
main
函数。创建包含以下内容的新源文件:

void dijkstra();
void output();

int main(int argc, char *argv[]) {
    dijkstra();
    output();
    return 0;
}
不带
main
的一体式cpp加上上面的这个文件,应该一起编译,并用一个源代码提供与以前相同的结果,就像它对我一样。如果忘记从算法文件中删除main,将出现
重复符号\u main
错误

不需要
静态


我在这里的回答没有涉及头文件的良好实践,也就是说,您希望在
.h
文件中包含这些函数声明。但它解决了编译时错误


<>你可能想找一本好书来帮助你通过C++的一些机器,程序上下文(在语言意义上)可以改变关键词的含义。这可能是令人困惑的,它证明了一个与C++一样丰富多彩历史的语言。查看书籍建议。

请提供头文件的完整上下文。我猜
dijkstra
应该是一个静态类?静态成员函数的定义不能包含关键字
static
。您试图定义
static void Alg::dijkstra(){
将立即触发
Alg.cpp
中的编译错误。但是,您报告了一个完全不同的错误。您声称此代码“工作正常”在
main.cpp
中。这意味着您发布了假代码。您的声明是不现实的。请发布真实代码。
Alg
?是什么?是类吗?是命名空间吗?@AndreyT Alg是类。请参见编辑。@user3063527:Er…您在pastebin中链接的代码与所讨论的问题完全不同。没有
alg
在该代码中。该代码中没有
static
。@Johnsyweb给我留下了一个“不能调用没有对象的成员函数”错误。可能
Initialize()
不是静态函数?你不能从静态函数调用实例函数,因为你不是从“内部”调用它一个对象。
Alg
很有可能是一个名称空间,而不是一个类……然而,OP仍然没有解释
Alg
到底是什么。@egur谢谢,现在更有意义了。谢谢你的帮助,我会看看你发布的内容。