【发布时间】:2014-01-09 17:03:04
【问题描述】:
我需要返回一个结构数组,然后将该数组传递给另一个打印方法。
这是我的 .h 文件:
#include <string>
#include "COMMON_TYPES.h"
#pragma once
#pragma pack(push, 1)
class ARS_HR_LINE_1
{
public:
struct ARS_HR_LINE_1_ALL
{
//Countre
int Counter
float Inner_Rate_FB_Fine;
float Inner_Rate_FB_Cross;
float Inner_Rate_FB_Roll;
float IMU_Yaw;
};
u16 img [1][616];
//Methods
ARS_HR_LINE_1::ARS_HR_LINE_1_ALL *Process_HR_ARS_Line_1();
void Print_HR_Line_1(FILE* fptr, int counter, ARS_HR_LINE_1::ARS_HR_LINE_1_ALL* h); // Print High Rate Line 1
};
#pragma pack(pop)
.cpp 文件:
#include <iostream>
#include "ARS_HR_LINE_1.h"
#include "COMMON_TYPES.h"
using namespace std;
ARS_HR_LINE_1::ARS_HR_LINE_1_ALL * ARS_HR_LINE_1::Process_HR_ARS_Line_1()
{
ARS_HR_LINE_1::ARS_HR_LINE_1_ALL h[60];
for(int i=0;i<60;++i)
{
h[i].Counter = (ARS_HR_LINE_1::img[0][i*10 + 0] << 16) + ARS_HR_LINE_1::img[0][i*10 + 1];
u32 inner_rate_fb_fine = (ARS_HR_LINE_1::img[0][i*10 + 2] << 16) + ARS_HR_LINE_1::img[0][i*10+3];
h[i].Inner_Rate_FB_Fine =*reinterpret_cast<float*>(&inner_rate_fb_fine);
u32 inner_rate_fb_cross = (ARS_HR_LINE_1::img[0][i*10+4] << 16) + ARS_HR_LINE_1::img[0][i*10+5];
h[i].Inner_Rate_FB_Cross =*reinterpret_cast<float*>(&inner_rate_fb_cross);
u32 inner_rate_fb_roll = (ARS_HR_LINE_1::img[0][i*10+6] << 16) + ARS_HR_LINE_1::img[0][i*10+7];
h[i].Inner_Rate_FB_Roll =*reinterpret_cast<float*>(&inner_rate_fb_roll);
u32 imu_yaw = (ARS_HR_LINE_1::img[0][i*10+8] << 16) + ARS_HR_LINE_1::img[0][i*10+9];
h[i].IMU_Yaw =*reinterpret_cast<float*>(&imu_yaw);
}
return h;
}
void ARS_HR_LINE_1::Print_HR_Line_1(FILE* fptr, int counter, ARS_HR_LINE_1::ARS_HR_LINE_1_ALL *h)
{
ARS_HR_LINE_1::ARS_HR_LINE_1_ALL temp;
temp.Counter = 0;
temp.Inner_Rate_FB_Fine = 0;
temp.Inner_Rate_FB_Cross = 0;
temp.Inner_Rate_FB_Roll = 0;
temp.IMU_Yaw = 0;
//tempArr[i] = *(h+i);
fprintf(fptr, "************************************************************\n");
fprintf(fptr, "******************IMAGE NUMBER %d ***************************\n", counter);
fprintf(fptr, "*********************LINE 1**********************************\n");
for(int i=0;i<60;++i)
{
temp = *(h+i);
//Counter and Filler - 4 bytes
fprintf(fptr, "Counter[%d] : %u\n", temp.Counter, i);
//Rate FB
fprintf(fptr, "Inner_Rate_FB_Fine[%d] : %12.20f\n", temp.Inner_Rate_FB_Fine, i);
fprintf(fptr, "Inner_Rate_FB_Cross[%d] : %12.20f\n", temp.Inner_Rate_FB_Cross, i);
fprintf(fptr, "Inner_Rate_FB_Roll[%d] : %12.20f\n", temp.Inner_Rate_FB_Roll, i);
//IMU_Yaw
fprintf(fptr, "IMU_Yaw[%d] : %12.20f\n", temp.IMU_Yaw, i);
}
}
然后我主要使用以下内容:
ARS_HR_LINE_1::ARS_HR_LINE_1_ALL* h;
ARS_HR_LINE_2::ARS_HR_LINE_2_ALL* h2;
h = ARS_HR_DEBUG_DATA.Process_HR_ARS_Line_1();
h2 = ARS_HR_DEBUG_DATA2.Process_HR_ARS_Line_2();
ARS_HR_DEBUG_DATA.Print_HR_Line_1(tassTxtFptr, i, h);
ARS_HR_DEBUG_DATA2.Print_HR_Line_2(tassTxtFptr, i, h2);
其中 tassTxtFptr 是指向文本文件的指针。
我的 Process_HR_ARS_Line_1 似乎工作正常,当我在 Visual Studio 中调出数组时,在返回它之前,所有值看起来都是正确的。当我去打印我的信息时,我得到了很多乱码,混合了正确的值。知道我做错了什么吗?
【问题讨论】:
-
使用
std:vector,写c++代码,而不是c-like代码。 -
注意你的编译器警告。它应该告诉你不应该返回指向局部变量的指针。
-
I need to return an array of structs and then pass this array to another method for printing.不,你没有。
标签: c++ arrays function struct