1

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:

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

3
  • Which line would be line 81 from your program? Commented Mar 19, 2009 at 14:23
  • What's the relation between steps 1, 2 and 3. I fail to see any? Commented Mar 19, 2009 at 14:26
  • That would be the TotalEnergy line. I managed to fix the issue though, turned out to be a missing * in the VecToArray function in the class header. Though the pointer to a local variable is a very good point, which leaves me with a far larger issue. Will open up a new question for it though. Commented Mar 19, 2009 at 14:27

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

Also, should CFE be returning an int or a double?
2

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.

Comments

2

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.

3 Comments

Depends on the compiler. It's valid C++0x and, I believe, GCC supports it already.
It is not valid C++0x. It is valid C99.
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.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.