Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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++_Oop - Fatal编程技术网

C++ 使用私有变量初始化数组

C++ 使用私有变量初始化数组,c++,oop,C++,Oop,我正在尝试做一个小应用程序,可以计算给定图形的一些路径 我创建了一个类来处理简单的图形,如下所示: class SimpleGraph { int _nbNodes; int _nbLines; protected: int AdjMatrix[_nbNodes, _nbNodes]; //Error happens here... int IncMatrix[_nbNodes, _nbLines]; //...and here! public

我正在尝试做一个小应用程序,可以计算给定图形的一些路径

我创建了一个类来处理简单的图形,如下所示:

class SimpleGraph {

    int _nbNodes;
    int _nbLines;

   protected:
    int AdjMatrix[_nbNodes, _nbNodes]; //Error happens here...
    int IncMatrix[_nbNodes, _nbLines]; //...and here!


   public:
    SimpleGraph(int nbNodes, int nbLines) { this->_nbNodes = nbNodes - 1; this->_nbLines = nbLines - 1; };
    virtual bool isSimple();
};
在编译时,我在两个受保护成员声明中得到一个错误

我不明白怎么回事,因为只有一个构造函数将这些值作为参数。因此,它们不能被取消初始化


这里缺少什么?

编译器需要知道为类
SimpleGraph
的成员分配多少空间。但是,由于
AdjMatrix
IncMatrix
是在堆栈上定义的,并且它们的大小是在运行时(即编译后)确定的,因此它无法做到这一点。具体来说,该标准规定类中数组的大小必须为

要解决此问题,您可以:

  • 在堆上分配
    AdjMatrix
    IncMatrix
    ,然后可以在运行时分配内存
  • 对两个数组使用固定大小,并将它们保留在堆栈上
--

代码的另一个主要问题是无法使用逗号创建多维数组(
AdjMatrix[int,int]
)。您必须改为使用:

  • AdjMatrix[int][int]
  • AdjMatrix[int*int]

    • 编译器需要知道为类
      SimpleGraph
      的成员分配多少空间。但是,由于
      AdjMatrix
      IncMatrix
      是在堆栈上定义的,并且它们的大小是在运行时(即编译后)确定的,因此它无法做到这一点。具体来说,该标准规定类中数组的大小必须为

      要解决此问题,您可以:

      • 在堆上分配
        AdjMatrix
        IncMatrix
        ,然后可以在运行时分配内存
      • 对两个数组使用固定大小,并将它们保留在堆栈上
      --

      代码的另一个主要问题是无法使用逗号创建多维数组(
      AdjMatrix[int,int]
      )。您必须改为使用:

      • AdjMatrix[int][int]
      • AdjMatrix[int*int]

      • C++中的对象具有固定大小,需要在编译时知道。在编译时,ADJMeMatt和InMatrix的大小不知道,只有在运行时。C++中的

        < P>对象具有固定大小,需要在编译时知道。AdjMatrix和InMatrix的大小在编译时是未知的,只有在运行时才知道。

        行中

        int AdjMatrix[_nbNodes, _nbNodes]; //Error happens here...
        int IncMatrix[_nbNodes, _nbLines]; //...and here!
        
        数组表示法是错误的。不能在C++中指定这样的2维数组。正确的符号在每个维度上使用括号,例如:

        int data[5][2];
        

        关于你所面临的问题,C++中数组的维数必须在编译时指定,即编译器必须知道编译程序时数组维数的值。这里显然不是这样。您必须还原为使用整数文本,如我的示例中所示,或者将代码更改为使用向量:

        std::vector<std::vector<int> > AdjMatrix;
        
        但是,由于您希望将它们作为构造函数参数传递,这可能不适合您的需要。尽管如此,如果您知道参数在编译时是常量,您可能可以在构造函数参数上使用C++11
        constexpr
        限定符

        int AdjMatrix[_nbNodes, _nbNodes]; //Error happens here...
        int IncMatrix[_nbNodes, _nbLines]; //...and here!
        
        数组表示法是错误的。不能在C++中指定这样的2维数组。正确的符号在每个维度上使用括号,例如:

        int data[5][2];
        

        关于你所面临的问题,C++中数组的维数必须在编译时指定,即编译器必须知道编译程序时数组维数的值。这里显然不是这样。您必须还原为使用整数文本,如我的示例中所示,或者将代码更改为使用向量:

        std::vector<std::vector<int> > AdjMatrix;
        

        但是,由于您希望将它们作为构造函数参数传递,这可能不适合您的需要。不过,如果您知道参数在编译时是常量,您可能可以在构造函数参数上使用C++11
        constexpr
        限定符。

        您必须使用动态内存分配
        new
        keyword@acrilige使用封装类(通常
        std::vector
        )强烈建议手动处理分配、复制安全和异常安全。您必须使用动态内存分配和
        new
        keyword@acrilige使用封装类(通常是
        std::vector
        )比手动处理分配、复制安全和异常安全更可取。