【问题标题】:passing a array varaible to function to return a array variable将数组变量传递给函数以返回数组变量
【发布时间】:2015-07-11 22:49:37
【问题描述】:

我正在尝试将数组传递给函数 (*getcreditcurve)。我期待函数 (*getcreditcurve) 返回一个数组。主函数预计将几个这样的数组发送到函数(*getcreditcurve),指针函数预计使用指针函数(*getcreditcurve)中给出的逻辑将数组返回给不同数组的主函数。但是我收到以下错误。有人可以帮忙解决问题吗?抱歉,我浏览了此站点中的其他帖子/问题,但无法找到解决此问题的最简单方法。我将使用此逻辑来构建其他项目,从而简化问题只是为了解决主要问题。

'#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>----- 构建开始:项目:Project2,配置:调试 Win32 ------ 1> 源.cpp 1>c:\users\kdatta\documents\cqf\c++\project2\source.cpp(12): error C3079: an initializer-list cannot be used as the right operation 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 operation of this assignment operator 1>c:\users\kdatta\documents\cqf\c++\project2\source.cpp(16): 错误 C2664: 'double *getcreditcurve(double)' : 无法将参数 1 从 'S1' 转换为 'double' 1> 没有可以进行这种转换的上下文 1>c:\users\kdatta\documents\cqf\c++\project2\source.cpp(17): 错误 C2664: 'double *getcreditcurve(double)' : 无法将参数 1 从 'S1' 转换为 'double' 1> 没有可以进行这种转换的上下文 1>c:\users\kdatta\documents\cqf\c++\project2\source.cpp(42): 警告C4172: 局部变量或临时返回地址 ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

【问题讨论】:

  • 我又来了 - 用缩进做 summat

标签: c++ arrays


【解决方案1】:

前向声明是

double *getcreditcurve(double);

而在你写的函数实现中

double *getcreditcurve(double S1[5])

意思是

double *getcreditcurve(double *);

将前向声明更改为:

double *getcreditcurve(double *);

修改C1C2初始化如下:

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

而不是S1 C1, C2

阅读Where can we use list initialization 了解原因。

警告!您正在返回一个局部变量

curve h;

设为static 或更改您的逻辑。

Run Live.

【讨论】:

    【解决方案2】:
    issuer I1 = getcreditcurve(C1); 
    

    getcreditcurve 将双指针作为参数,但在声明中您只提到了 double..new 声明将是

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

    定义是

    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;
    }'
    

    【讨论】:

    • @rakeb.void。感谢你们两位的快速回复。我根据您的两个建议更改了我的程序。但是,我不希望将其放入 excel 中。我希望 I+1 到 3 0.0039 = 0.0117 并且 I+2 到 4 0.0060 = 0.0024 看不到我在调试中也看不到任何错误。你能帮忙吗
    猜你喜欢
    • 2020-07-28
    • 2017-09-12
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多