【发布时间】:2015-01-05 14:41:35
【问题描述】:
我有一个在 types.h 中定义的结构体,代码如下:
struct data_Variant {
FlightPlanSteeringDataRecord steeringData;
FlightPlanType flightPlan : 8;
MinitoteLegDataType legDataType : 8; // discriminent, either current or amplified
unsigned spare : 16;
union {
// currentLeg =>
CurrentLegDataRecord currentLegData;
// amplifiedLeg =>
AmplifiedLegDataRecord amplifiedLegData;
} u;
};
然后,我尝试将该结构的实例作为参数传递给名为 dialog.cpp 的 C++ 源文件中的函数:
void dialogue::update( const types::data_Variant& perfData){
...
}
我现在想在这个update() 函数中更改该结构的一些成员的值。但是,如果我像往常一样尝试这样做,即
perfData.etaValid = true;
我收到一个编译错误,上面写着:“C2166:l-value 指定 const 对象”。据我了解,这是因为 perfData 已被声明为常量变量。我这样想对吗?
由于我没有写这部分代码,只是想用它来更新GUI上显示的值,我并不想通过删除const关键字来更改perfData变量,以防我破坏其他东西。有什么办法可以改变已经声明为 const 的变量的值?
我尝试在代码的另一部分声明相同的结构变量,而不使用 const 关键字,看看我是否可以更改其中一些成员的值......即在 Interface.cpp 中,我已将以下代码添加到名为 sendData() 的函数中:
types::data_Variant& perfData;
perfData.steering.etaValid = true;
perfData.steering.ttgValid = true;
但是,我现在在这些行上得到以下编译错误:
error C2653: 'types' is not a class or namespace name
error C2065: data_Variant: undeclared identifier
error C2065: 'perfData': undeclared identifier
error C2228: left of '.steering' must have class/ struct/ union
有没有办法更新这个结构的值?如果是这样,我应该怎么做,我在这里做错了什么?
我已将以下函数添加到dialogue.cpp 源文件中,如答案中所建议的:
void dialogue::setFPTTGandETAValidityTrue(
FlightPlanMinitoteTypes::FlightPlanMinitoteData_Variant& perfData)
{
SESL_FUNCTION_BEGIN(setFPTTGandETAValidityTrue)
perfData.steeringData.fpETAValid = true;
perfData.steeringData.fpTTGValid = true;
SESL_FUNCTION_END()
}
【问题讨论】: