类的公共成员值没有改变
Public member value of a class is not changing
我有一个名为"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
数组,而不是声明为双精度数组吗?在任何情况下,在类中有两个不同的数据成员k
和K
根本不是一个好主意,除非您有非常令人信服的理由这样命名它们。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 对列表中的每个对象调用成员函数,以改变对象所在的列表
- 为什么调用 CUDA 内核函数时这个类成员变量没有改变
- 当函数应该改变对象的状态时,是选择成员函数还是朋友函数
- 类的公共成员值没有改变
- this指针在调用成员函数时改变
- 被非const成员函数改变的可变变量
- 静态方法中的对象创建是改变其他静态对象的私有成员变量
- 基于单独的模板化成员改变类的模板化成员函数的返回类型
- c++单元测试:改变成员范围
- 班级成员不情愿地改变了
- 为什么cocos2d-x schedule改变了成员对象
- 当调用改变它的成员函数时,不改变C对象成员