| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1768 人关注过本帖
标题:如何向主函数返回一个复数数组
只看楼主 加入收藏
Lidong005
Rank: 1
等 级:新手上路
帖 子:34
专家分:1
注 册:2010-9-1
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:5 
如何向主函数返回一个复数数组
请教各位,如何将程序中计算出的复数数组返回给主函数。
 以下是我的部分代码,请各位高手指出以下我的错误所在。谢谢了。
程序代码:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <complex>
#include <iomanip>
#include <fstream>
#include <conio.h>
#include <math.h>
#include <fftw3.h>
#include <stdio.h>
#define M 128
#define N 128
#define Pi 3.1415926535898 
#define complex_d complex<double>


complex_d BPalgorithm(complex_d G[M][N])
{
complex_d *gg;
complex_d PLm[M][N];
fftw_complex *in, *out;
fftw_plan p;

   
//分配存储空间
gg=new complex_d[M*N];
if(gg==NULL)
{
throw "分配内存失败!";
}
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * M);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * M);

// 设置变换计划
p = fftw_plan_dft_1d(M, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
   
// 设置测试数据
  for(int j=0;j<N;j++)
{
for(int i=0;i<M;i++)
{
  in[i][0]=real(G[i][j]);
  in[i][1]=imag(G[i][j]);
}

fftw_execute(p);  

for(int l=0;l<M;l++)
{
PLm[l][j]=complex_d(out[l][0],out[l][1]);
}

}

gg=PLm; //出现错误(“=”:无法从“std::complex<double> [128][128]”转换“std::complex<double>*”)
return gg;

// 释放内存
delete []gg;
gg = NULL;
  fftw_destory(p);
  fftw_free(in);
fftw_free(out);
}




int main()
{
complex_d G[M][N];
complex_d *g;

 
//分配存储空间
g=new complex_d[M*N];
if(g==NULL)
{
throw "分配内存失败!";

}
  ......

  g=BPalgorithm(G);//这里出现了错误
  ...,...
  }



搜索更多相关主题的帖子: 复数 函数 
2010-10-19 21:46
jack10141
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:陕西西安
等 级:小飞侠
威 望:6
帖 子:706
专家分:2271
注 册:2010-8-10
收藏
得分:7 
汗,你又不给所有的代码,你让别人怎么帮你找问题?用眼睛扫描么??无语了

Coding就像一盒巧克力,你永远不会知道你会遇到什么BUG
别跟我说你是不能的,这让我愤怒,因为这侮辱了你的智慧
2010-10-19 22:43
zhongying822
Rank: 2
等 级:论坛游民
帖 子:16
专家分:17
注 册:2010-10-11
收藏
得分:6 
以数组名做参数,不需要返回,或开辟为全局数组
2010-10-20 06:54
Lidong005
Rank: 1
等 级:新手上路
帖 子:34
专家分:1
注 册:2010-9-1
收藏
得分:0 
回复 2楼 jack10141
呵呵,疏忽了。
下面是我的代码。
程序代码:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <complex>
#include <iomanip>
#include <fstream>
#include <conio.h>
#include <math.h>
#include <fftw3.h>
#include <stdio.h>

#define M 128
#define N 128
#define Pi  3.1415926535898
#define complex_d complex<double>
double data[M][2*N];

using namespace std;

void showresult(complex_d G[M][N])
{
    FILE* data = NULL;
    fopen_s(&data, "imagedata.txt", "w");
    if(data == NULL)
    {
        throw "创建数据文件失败。";
    }

    for(int i=0;i<M;i++)
    {
        for(int j=0;j<N;j++)
        {
            fprintf_s(data, "%f\t",10*log10(abs(G[i][j])));
        }
        fprintf_s(data, "\n");
    }
    fclose(data);
}


void dataread( )
{
    using namespace std;
    int a[2];
    double b[N+1];
    double c[1];
    streampos weizhi;
    streampos WZhi;


    ifstream infile("ISAR2D[1].dat",ios::in);

    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    for(int i=0;i<2;i++)
    {
        infile>>a[i];
    }
    //cout<<endl;
    streampos weizhi1 =infile.tellg();                //weizhi1 是 64 64


    infile.seekg(weizhi1);

    for(int i=0;i<N+1;i++)
    {
        infile>>b[i];
    }
    streampos weizhi2 =infile.tellg();               //weizhi2 第二行结束

    infile.seekg(weizhi2);
    infile>>c[0];
    streampos weizhi3=infile.tellg();               //weizhi3 第三行第二个数开始位置
    weizhi=weizhi3-weizhi2;                        //weizhi 求出第一个数所占的空间



    WZhi= weizhi3;
    for(int i=0;i<M;i++)
    {  
        infile.seekg(WZhi);
        for(int j=0;j<2*N;j++)
        {
            infile>>data[i][j];
        }
        streampos weizhi4=infile.tellg(); 
        infile.seekg(weizhi4);
        infile>>c[0];
        streampos weizhi5=infile.tellg(); 
        WZhi=weizhi5;
    }

}

complex_d BPalgorithm(complex_d G[M][N])
{
    complex_d *gg;
    complex_d PLm[M][N];
    fftw_complex *in, *out;
    fftw_plan p;

   
    //分配存储空间
    gg=new complex_d[M*N];
    if(gg==NULL)
    {
        throw "分配内存失败!";
    }
    in  = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * M);
    out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * M);
   
    // 设置变换计划
    p = fftw_plan_dft_1d(M, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
   
    // 设置测试数据
    for(int j=0;j<N;j++)
    {
        for(int i=0;i<M;i++)
        {
          in[i][0]=real(G[i][j]);
          in[i][1]=imag(G[i][j]);
        }

        fftw_execute(p);
       
        for(int l=0;l<M;l++)
        {
            complex_d a(out[l][0],out[l][1]);
            PLm[l][j]=a;
        }
        

    }

    gg=PLm;
    return gg;
   
    // 释放内存
    delete []gg;
    gg = NULL;
    fftw_destroy_plan(p);
    fftw_free(in);
    fftw_free(out);
}




int main()
{
    complex_d G[M][N];
    complex_d a;
    complex_d *g;
    complex_d PL[M];
    double  Win[M][N];

    //分配存储空间
    g=new complex_d[M*N];
    if(g==NULL)
    {
        throw "分配内存失败!";
    }
    //读取数据
    dataread( );

    for (int i=0; i<M; i++)
    {
        for (int j=0; j<N; j++)
        {
            complex_d a(data[i][j],data[i][j]);
            G[i][j]=a;
        }
    }
    //hamming窗
    for (int i=0; i<M; i++)
    {
        for (int j=0; j<N; j++)
        {
            Win[i][j]=(0.5-0.5*(cos(2*Pi*j/(8*M-1))))*(0.5-0.5*(cos(2*Pi*i/(8*N-1))));
        }
    }
    //时域加窗
    for (int i=0; i<M; i++)
    {
        for (int j=0; j<N; j++)
        {
            G[i][j]=G[i][j]*Win[i][j];
        }
    }

    g=BPalgorithm(G);
    

    // 显示测试结果
    showresult(G);

}
2010-10-20 08:47
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:7 
    complex_d *gg;
    complex_d PLm[M][N];
一个二维数组 一个单指针 怎么能=呢?
complex_d *g;
g=BPalgorithm(G);返回一个complex_d 你怎么能用complex_d *接收呢
2010-10-20 10:06
Lidong005
Rank: 1
等 级:新手上路
帖 子:34
专家分:1
注 册:2010-9-1
收藏
得分:0 
回复 5楼 hahayezhe
我想的是,把这个二维数组complex_d PLm[M][N]的地址赋给*gg,然后返回这个地址。
这样可行否?
2010-10-20 16:25
快速回复:如何向主函数返回一个复数数组
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015599 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved