i wrote this c++ code to make a function returns a pointer to an array of double, in such way i use it as rvalue. i got a weird error message because i couldn't understand what's wrong with it. here is the code with the error message
#include <iostream>
using std::cout;
using std::endl;
double* fct_returns_ptr(double, int); // function prototype
int main(void)
{
double test_array[] = { 3.0, 10.0, 1.5, 15.0 }; // test value
int len = (sizeof test_array)/(sizeof test_array[0]);
//double* ptr_result = new double(0.0); //[len] pointer to result
double* ptr_result = new double[len]; // (0.0) pointer to result
ptr_result = fct_returns_ptr(test_array, len);
for (int i=0; i<len; i++)
cout << endl << "Result = " << *(ptr_result+i); // display result
cout << endl;
delete [] ptr_result; // free the memory
return 0;
}
// function definition
double* fct_returns_ptr(double data[], int length)
{
double* result = new double(0.0);
for (int i=0; i<length; i++)
*(result+i) = 3.0*data[i];
return result;
}
/*
C:\Users\laptop\Desktop\C_CPP>cl /Tp returns_ptr.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.40219.01 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
returns_ptr.cpp
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : warning C4530: C++
exception handler used, but unwind semantics are not enabled. Specify /EHsc
returns_ptr.cpp(13) : error C2664: 'returns_ptr' : cannot convert parameter 1 from 'double [4]' to 'double'
There is no context in which this conversion is possible
*/
Try this , What's the problem was there ? Your function prototype and signature didn't match .