gpt4 book ai didi

c++ - 非静态数据成员的无效使用

转载 作者:搜寻专家 更新时间:2023-10-30 23:55:00 25 4
gpt4 key购买 nike

class matrix
{
public:
int m;
int n;
int mat[m][n];
};

我收到这个错误:

[Error]: Invalid use of non static data member 'matrix::n'
On declaring static:

class matrix
{
public:
static int m;
static int n;
int mat[m][n]; //Error
};

我收到这个错误:

[Error]: Array bound is not an integer constant before ']' token
Please tell me what these errors mean and how to fix this problem.

最佳答案

C++ 中数组的大小必须是编译时可计算的。

编译器不知道如何处理 int mat[m][n];由于 m 的值和 n在编译时是未知的。

如果您想要一个良好可靠的矩阵类,请考虑使用 Boost 中的 BLAS 库。 std::vector<std::vector<int>> 可以工作,但它是一个锯齿状矩阵,内存模型很差。

关于c++ - 非静态数据成员的无效使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34197083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com