-1

I have a struct which is defined in types.h with the following code:

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;

};

I am then trying to pass an instance of that struct as a parameter to a function in a C++ source file called dialogue.cpp:

void dialogue::update( const types::data_Variant& perfData){
...
}

I now want to change the value of some of the members of that struct inside this update() function. However, if I try doing this as I usually would, i.e.

perfData.etaValid = true;

I get a compile error which says: "C2166: l-value specifies const object". As I understand, this is because perfData has been declared as a constant variable. Am I correct in thinking this?

Since I didn't write this part of the code, but only want to use it to update the value displayed on the GUI, I don't really want to change the perfData variable by removing the const keyword, in case I break something else. Is there any way to change the value of a variable that has been declared as const?

I have tried declaring the same struct variable in another part of the code, without using the const keyword, to see if I can change the values of some of its members there... i.e. in Interface.cpp, I have added the following code to a function called sendData():

types::data_Variant& perfData;
perfData.steering.etaValid = true;
perfData.steering.ttgValid = true;

However, I now get the following compile errors on these lines:

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

Is there a way of updating the values of this struct? If so, how should I do it, and what am I doing wrong here?

I have added the following function to the dialogue.cpp source file, as suggested in the answer:

void dialogue::setFPTTGandETAValidityTrue(
FlightPlanMinitoteTypes::FlightPlanMinitoteData_Variant& perfData)
{
SESL_FUNCTION_BEGIN(setFPTTGandETAValidityTrue)
    perfData.steeringData.fpETAValid = true;
    perfData.steeringData.fpTTGValid = true;
SESL_FUNCTION_END()
}
Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118

2 Answers2

2

You could add a wrapper for yourself.

void myupdate(dialogue& dia, types::data_Variant& perfData)
{
    perfData.etaValid = true;
    dia.update(perfData);
}

Then call myupdate() instead of dialogue::update().

timrau
  • 22,578
  • 4
  • 51
  • 64
  • Hey, thanks for your answer. I've tried giving what you suggested a go, and added a function to `dialogue.cpp` to do what you had suggested... (see updated OP for code for new function) However, this is now giving me several compiler errors, such as: "Error 1 error C2039: 'setFPTTGandETAValidityTrue' : is not a member of 'FlightPlanAmpMinitote' " – Noble-Surfer Jan 05 '15 at 15:18
  • Did you add the function prototype into header file? Also, we don't know what `SESL_FUNCTION_BEGIN()` and `SESL_FUNCTION_END()` are. – timrau Jan 05 '15 at 15:22
1

You declare

void dialogue::update( const types::data_Variant& perfData){
   ...
}

that const is a declaration of you saying: "I won't modify the referenced object in this function". If you want to modify it in dialogue::update you have to remove the const keyword. Wrapping is not a solution, in my opinion, makes the code harder to maintain. Also I vote against remove const with const_cast.

The correct solution is to remove const from method declaration if you want to modify the referenced object inside that function.

Heto
  • 590
  • 4
  • 9
  • This was the first thing I tried, but it gave me a compile error that says: "Overloaded member function not found... " on the line where I call that function. – Noble-Surfer Jan 05 '15 at 15:25
  • You have to remove `const` twice: from the declaration (probably in a `.h` file) and the definition (probably in a `.cpp` file). – TonyK Jan 05 '15 at 15:28
  • you have to modify also the declaration from header file. – Heto Jan 05 '15 at 15:29
  • Ok, thanks- will give that a go. Will removing `const` affect any of the other things that the function does, or the values of the other data held in `perfData` in anyway? – Noble-Surfer Jan 05 '15 at 15:41
  • No. That `const` is just for clearness, when you don't want to change the passed object in the function. – Heto Jan 05 '15 at 21:40