C ++ Dynamic float在构造函数中死亡?

C++ Dynamic float dies in the constructer?

我的字段中需要一个浮点数组 class。 一切正常,现在我想让我的高度图更加动态。

我刚刚发现它在我这样做时死了:

*highMap = new float[mapWidth];
for (int x = 0; x<GFX_Rx; x++)
{
    *highMap[x]=30;
}

头文件中的highmap

float* highMap[];

谢谢

您的代码中存在一些问题。首先你不能声明一个动态大小的数组,它甚至不应该编译。其次,你不能在不知道指针指向某个有效位置的情况下取消引用它,你会遇到段错误。

如果你不想使用std::vector,你可以这样做:

#include <iostream>

const int mapWidth = 5;
float** highMap;
int GFX_Rx=5;

int main() 
{
    highMap = new float*[mapWidth];
    for (int x = 0; x<GFX_Rx; x++)
    {
        highMap[x]=new float(30);
    }
}