错误:"*"的操作数必须是指针

Error: operand of '*' must be a pointer

本文关键字:指针 错误 操作数      更新时间:2023-10-16

我似乎在我的代码中遇到了这个错误,当我将鼠标悬停在说"数据"的位上时,它说"错误:"*"的操作数必须是一个指针"。谁能看出问题出在哪里。

double* Matrix::get(int i, int j) const
    {
        return *data[i*N + j];
    }

假设data是一个数组成员变量,你需要说的是:

return &data[i*N + j];

此外,由于您的函数const您应该返回一个 const 指针:

const double *Matrix::get(int i, int j) const
{
    return &data[i*N + j];
}

如果您不希望调用方修改数组中的值,请将其更改为:

double Matrix::get(int i, int j) const
{
    return data[i*N + j];
}

如果 data 是 double 数组,则删除 *,因为您不需要返回内存地址:

double Matrix::get(int i, int j) const
{
    return data[i*N + j];
}