小编yar*_*oul的帖子

如何在类中声明和初始化静态成员?

当我编译包含以下头文件的代码时,出现错误消息:

Graph.h:22: error: ISO C++ forbids in-class initialization of non-const 
static member `maxNumberOfNeighbors'
Run Code Online (Sandbox Code Playgroud)

如何声明和初始化不是const的静态成员?

这是.h文件

#ifndef GRAPH_H
#define GRAPH_H

typedef char ElementType;
class Graph {
public:
    class Node {
    public:
        static int maxNumberOfNeighbors = 4;;
        int numberOfNeighbors;
        Node * neighbors;
        ElementType data;
        Node();
        Node(ElementType data);
        void addNeighbor(Node node);
    };

typedef Node* NodePtr;

Graph();
void addNode(Node node);
NodePtr getNeighbors(Node node);
bool hasCycle(Node parent);
private:
    NodePtr nodes;
    static int maxNumberOfNodes;
    int numberOfNodes;
};

#endif /* GRAPH_H */
Run Code Online (Sandbox Code Playgroud)

c++ initialization static-members

1
推荐指数
1
解决办法
112
查看次数

标签 统计

c++ ×1

initialization ×1

static-members ×1