简体   繁体   English

从指向数组的 function 返回指针时出错

[英]Error in returning a pointer from a function that points to an array

I'm in a bit of a fiddle in that I don't know why my code brings up the following error when compiling:我有点不知所措,因为我不知道为什么我的代码在编译时会出现以下错误:

1>..\SA.cpp(81) : error C2664: 'CFE' : cannot convert parameter 1 from 'int' to 'int []'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

Essentially I am trying to:本质上,我正在尝试:

Step1: Convert from a vector to an array using: Step1:使用以下方法将向量转换为数组:

int* VecToArray(vector<int> Vec)
{
    int ary[Vec.size()];

    for(int i = 0; i < Vec.size(); i++)
        ary[i] = Vec[i];

    return ary;
}

Step2: Calling upon a function into which the array is an paremeter and it returns a pointer from a newly generated array:步骤2:调用一个function,其中数组是一个参数,它从新生成的数组中返回一个指针:

int* CFE(int density[])
{
   ...do stuff to generate 'double Energy[]'....

    return Energy;
}

Step 3: Using this pointer in a third function to calcualte the sum of Energy[]:第 3 步:在第三个 function 中使用此指针来计算 Energy[] 的总和:

double ObjFunction (double *E_Array) 
{
    double SumEnergy = 0;

    int n = 10; // Is irrelivant

    for (int i = 0; i < n; i++)
    {
        SumEnergy += E_Array[i];
    }

    return SumEnergy;
}

To make for simpler coding I've used the functions like so, where VectorName is an integer vector:为了简化编码,我使用了这样的函数,其中 VectorName 是 integer 向量:

double TotalEnergy = ObjFunction ( CFE ( VecToArray ( VectorName ) ) );

I am obviously getting the parameter types wrong somewhere, though I just cant see why myself.我显然在某个地方弄错了参数类型,尽管我自己也不明白为什么。 Could anyone with a more experienced eye assist in spotting it/them?任何有经验的人都可以帮助发现它/它们吗?

Where does Energy come from? Energy从何而来? If it's a double[] then you can't just cast it to an int* .如果它是一个double[]那么你不能把它转换成一个int*

std::vector<int> is guaranteed to be contiguous, so if you want to convert a std::vector<int> VectorName to an const int* use &VectorName[0] . std::vector<int>保证是连续的,因此如果要将std::vector<int> VectorName转换为const int*使用&VectorName[0] If, on the other hand, your CFE function modifies the array is passed, it's probably better off creating it locally.另一方面,如果您的 CFE function 修改了数组,则最好在本地创建它。

Not sure about the compiler error, but you are going to have big problems if you return a local array from a function.不确定编译器错误,但如果您从 function 返回本地数组,您将遇到大问题。

Step 1 has several problems:第 1 步有几个问题:

  • You are trying to create an array with a variable size.您正在尝试创建一个可变大小的数组。 You cannot do this in C89 or C++ ( I think C99 adds this).您不能在 C89 或 C++ 中执行此操作(我认为 C99 添加了此内容)。
  • You are returning a pointer to a local variable that has gone out of scope.您正在返回一个指向已超出 scope 的局部变量的指针。

You should fix this:你应该解决这个问题:

int ary[Vec.size()];

To:至:

int *ary = new int(Vec.size());

Compiler does not know Vec.size() at compile time, so it cannot create an array.编译器在编译时不知道 Vec.size(),因此它无法创建数组。

Make sure you free the memory later.确保稍后释放 memory。

That's to fix the problem.那就是解决问题。 But, I think your approach is not good either.但是,我认为你的方法也不好。 Vector is has almost the same performance as regular array, but is much safer and easier to use. Vector 与常规数组的性能几乎相同,但更安全,更易于使用。 Why don't you just use vectors?为什么不直接使用向量?

Even if you decide to use arrays, passing them by value is not very efficient.即使您决定使用 arrays,按值传递它们也不是很有效。 Especially since you redirect the output to another function immediately.特别是因为您立即将 output 重定向到另一个 function。 I would rather use references for this kind of problem.我宁愿使用参考来解决这类问题。

You can't create an array with a dynamically-evaluated size.您不能创建具有动态评估大小的数组。 You can't return a locally-defined array.您不能返回本地定义的数组。

You can, however, consider &myVector[0] as an array.但是,您可以将 &myVector[0] 视为一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM