c++ - 动态结构错误,指针转换

标签 c++ arrays pointers dynamic-allocation

我在结构的构造函数中遇到此错误。为什么我得到它,因为我只使用 * 指针而不是 **。

错误:

\ListStruc.cpp:26:25: error: cannot convert 'int**' to 'int*' in assignment

结构.h

struct Arr{

    int days;
    int *M;
};
typedef Arr* Array;

结构.cpp

void constr(Array &o){
    //Construct of 1*31 Matrix
    o=new Arr;
    o->days = days;
    o->M = new int*[o->days];

最佳答案

因为 M 是一个 int*,正确的初始化应该是:

o->M = new int[o->days];

关于c++ - 动态结构错误,指针转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10112787/

相关文章:

c++ - 模板类方法中的递归 std::function 定义

c++ - 函数指针给我 “redeclared as different kind of symbol” 错误

java - 按每个字符串中的最后一个逗号拆分数组中的字符串

c - Arduino - 获取响应和解析 json 数组的正确方法

C语言 : how to interpret this #define?

c - 指针和双指针练习

c++ - ClassName objectName(4); 有什么区别?和 ClassName objectName = 4;

c++ - C++ 会破坏给定化合物中的对象吗?

c++ - 在 C++ 函数参数中使用 & 运算符 : pass-by-rvalue-reference

sql - 具有多个 unnest 调用的 ROWS FROM() 中的列定义