类的公共成员值没有改变

Public member value of a class is not changing

本文关键字:改变 成员      更新时间:2023-10-16

我有一个名为"Turns"的类,它的所有成员都是public。到目前为止,我还没有编辑过这个类的成员的值(只读取),但是我已经在其他头文件中更改了其他类的值,没有问题。问题是,现在我想改变一个值,它似乎不工作,无论我如何尝试。

Turns.h

#ifndef TURNS_H
#define TURNS_H
#include "Constants.h"
using namespace Constants;
#include <string>
using namespace std;
#include "utilities.h"
class Turns
{
public: 
    Turns();
    void insertk(double val, int i);
    int k[5]; // K denotes the fixed radius for R turns
    double K[4];
    string TPT; // F for fly-by turns. R for fix turns.
    int headings[5]; // Measured from Arrival Airport Threshold
    static const int hdgSIZE=5;
    double thr2wpt[4]; // Measured from Arrival Airport Threshold
    int phi_max[4];
    double turnrate_max[4];
    double scale;
    double Deltachig[4]; // Angle changed
    double phi_end[4]; // Estimated phi at the end of the turn
    double turnrate[4];
    int iend[4]; // Starting distance
};
#endif

Turns.cpp

#include "Turns.h"
Turns::Turns()
{
    k[0]=0; k[1]=0; k[2]=0; k[3]=0; k[4]=0;
    TPT="FFFFF";
    headings[0]=35; headings[1]=60; headings[2]=80; headings[3]=50; headings[4]=40;
    thr2wpt[0]=-50*nm2m; thr2wpt[1]=-150*nm2m; thr2wpt[2]=-350*nm2m;   thr2wpt[3]=-500*nm2m;
    phi_max[0]=16; phi_max[1]=23; phi_max[2]=22; phi_max[3]=1;
    turnrate_max[0]=1.5; turnrate_max[1]=3.0; turnrate_max[2]=3.0; turnrate_max[3]=3.0;
    scale=1.5;
    // We finish to fill
    for(int n=0; n<(sizeof(headings)/sizeof(headings[0])-1); n++)
    {
        K[n]=0;
        Deltachig[n]=headings[n+1]-headings[n];
        if(abs(Deltachig[n])>180)
            Deltachig[n]=Deltachig[n]-360*sign(Deltachig[n]);
        phi_max[n]=sign(Deltachig[n])*phi_max[n];
        phi_end[n]=phi_max[n]/scale;
        turnrate[n]=sign(Deltachig[n])*turnrate_max[n];
        iend[n]=0;
    }
}
void Turns::insertk(double val, int i)
{
    this->k[i]=val;
}

不工作的部分是函数"insertk"。例如,在主程序中,val=0.00023, I =0,"k"的值不变。在主要(简化,因为它是一个大程序的一部分)我有:

Turns * tns;
tns=new Turns();
tns->insertk(0.00023, 0);

如果我尝试直接替换值(这应该工作,因为成员是公共的),结果是相同的:

tns->k[0]=0.00023

PS:我知道成员应该被保护,但是我读了很多,我想知道为什么"k"的值没有改变

那么,您的k数组被声明为整数数组。如果你将0.00023赋给一个整数,它将被四舍五入(截断)到0。因此,这看起来好像k[0]的值没有变化。它最初是0,赋值后仍然是0。然而,任务已经完成了。

为什么要把0.00023赋值给一个整数?难道你不应该使用你的K数组,而不是声明为双精度数组吗?在任何情况下,在类中有两个不同的数据成员kK根本不是一个好主意,除非您有非常令人信服的理由这样命名它们。