views:

385

answers:

5

Greetings Everyone,

I'm in a bit of a fiddle in that I dont 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:

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:

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[]:

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 interger vector:

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 experianced eye assist in spotting it/them?

Thank you.

+3  A: 

Not sure about the compiler error, but you are going to have big problems if you return a local array from a function.

Maurice Perry
Also, should CFE be returning an int or a double?
Sam Hoice
+2  A: 

Step 1 has several problems:

  • You are trying to create an array with a variable size. You cannot do this in C89 or C++ ( I think C99 adds this).
  • You are returning a pointer to a local variable that has gone out of scope.
Greg Rogers
+2  A: 

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.

Make sure you free the memory later.

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. Why don't you just use vectors?

Even if you decide to use arrays, passing them by value is not very efficient. Especially since you redirect the output to another function immediately. I would rather use references for this kind of problem.

Milan Babuškov
Depends on the compiler. It's valid C++0x and, I believe, GCC supports it already.
Ferruccio
It is not valid C++0x. It is valid C99.
Nemanja Trifunovic
Nemanja is right. There is something that looks similar in that if you have a constexpr function, you can use it to create an array - but vector::size will not be a constexpr function.
Richard Corden
+4  A: 

Where does Energy come from? If it's a double[] then you can't just cast it to an 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]. If, on the other hand, your CFE function modifies the array is passed, it's probably better off creating it locally.

Pete Kirkham
+1  A: 

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.

Benoît