2

I am trying to pass array to function (*getcreditcurve). I am expecting function (*getcreditcurve) to return an array. Main function is expected to send several such array to function (*getcreditcurve), pointer function is expected to return a array to main function for different array using the logic given in pointer function (*getcreditcurve). however I get following error. Can somebody help in trouble shooting please? Sorry I went through other post/question in this site but not able to get simplest way to solve this issue. I am going to use this logic to build other projects so simplified the question just to resolve main issue.

'#include<iostream>
#include<cmath>
#include<fstream>
typedef double S1[5];
using namespace std;
double *getcreditcurve(double);

int main()
{
S1 C1, C2;

C1 = { 0.0029, 0.0039, 0.0046, 0.0052, 0.0057 };
C2 = { 0.0020, 0.0050, 0.0060, 0.0070, 0.0080 };

typedef double *issuer;
issuer I1 = getcreditcurve(C1);
issuer I2 = getcreditcurve(C2);


ofstream print;
print.open("result1.xls");
print << I1+1 << '\t' << I2+2 << endl;
print.close();
return 0;
}

double *getcreditcurve(double S1[5])
   {
const int cp = 5;
typedef double curve[cp];
curve h;

h[0] = 2 * S1[0];
h[1] = 3 * S1[1];
h[2] = 4 * S1[2];
h[3] = 5 * S1[3];
h[4] = 6 * S1[4];

return h;
 }'

1>------ Build started: Project: Project2, Configuration: Debug Win32 ------ 1> Source.cpp 1>c:\users\kdatta\documents\cqf\c++\project2\source.cpp(12): error C3079: an initializer-list cannot be used as the right operand of this assignment operator 1>c:\users\kdatta\documents\cqf\c++\project2\source.cpp(13): error C3079: an initializer-list cannot be used as the right operand of this assignment operator 1>c:\users\kdatta\documents\cqf\c++\project2\source.cpp(16): error C2664: 'double *getcreditcurve(double)' : cannot convert argument 1 from 'S1' to 'double' 1> There is no context in which this conversion is possible 1>c:\users\kdatta\documents\cqf\c++\project2\source.cpp(17): error C2664: 'double *getcreditcurve(double)' : cannot convert argument 1 from 'S1' to 'double' 1> There is no context in which this conversion is possible 1>c:\users\kdatta\documents\cqf\c++\project2\source.cpp(42): warning C4172: returning address of local variable or temporary ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Kausik
  • 43
  • 5

2 Answers2

1

The forward declaration is

double *getcreditcurve(double);

whereas in the function implementation you wrote

double *getcreditcurve(double S1[5])

which means

double *getcreditcurve(double *);

change forward declaration as:

double *getcreditcurve(double *);

Change C1 and C2 initialization as following:

S1 C1 = { 0.0029, 0.0039, 0.0046, 0.0052, 0.0057 };
S1 C2 = { 0.0020, 0.0050, 0.0060, 0.0070, 0.0080 };

instead of S1 C1, C2;

Read Where can we use list initialization to know why.

Warning! you are returning a local variable

curve h;

Make it static or change your logic.

Run Live.

Community
  • 1
  • 1
mazhar islam
  • 5,561
  • 3
  • 20
  • 41
0
issuer I1 = getcreditcurve(C1); 

getcreditcurve takes double pointer as an argument but in declaration you only mentioned double..new declaration will be

double *getcreditcurve(S1); or double *getcreditcurve(double *)

and definition would be

double *getcreditcurve(S1 ptr)
{
 const int cp = 5;
 typedef double curve[cp];
 curve h;

 h[0] = 2 * ptr[0];
 h[1] = 3 * ptr[1];
 h[2] = 4 * ptr[2];
 h[3] = 5 * ptr[3];
 h[4] = 6 * ptr[4];

 return h;
}'
Naresh
  • 1,842
  • 2
  • 24
  • 36
  • @rakeb.void. Thanks for quick response to both of you. I changed my program as per both of your advice. However I don't get desired out put in excel. I expect I+1 to by 3 *0.0039 = 0.0117 and I+2 to 4* 0.0060 = 0.0024 don't see that I don't see any error also in debug. Can you please help – Kausik Jul 11 '15 at 15:47