我需要返回一個結構數組,然後將這個數組傳遞給另一個打印方法。在C++中返回一個結構數組,並將該數組傳遞給另一個方法?
這裏是我的.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'類代碼。 – StoryTeller
注意你的編譯器警告。它應該告訴你,你不應該返回一個指向局部變量的指針。 –
'我需要返回一個結構數組,然後將這個數組傳遞給另一種打印方法。「不,你不知道。 –