I get error constant expression required

蹲街弑〆低调 提交于 2019-12-11 14:39:50

问题


I'm using this inside a Header File:

typedef struct
{
 int DefaultValue;

}SetValues;

extern volatile SetValues XronosTrofodosias;

And this inside my Source file:

volatile SetValues XronosTrofodosias;

int  *DefaultValuesWaterSubMenu[]={XronosTrofodosias.DefaultValue};

Why i get this error? How can i fix that?

Here is my Real String

int  *DefaultValuesWaterSubMenu[]={XronosTrofodosias.DefaultValue,XronosAdranias.DefaultValue,XronosTrofodosiasHighTemp.DefaultValue,Xronos1hsRipsis.DefaultValue,Xronos2hsRipsis.DefaultValue,Kathisterisi2hsRipsis.DefaultValue,Energopoihsi2hsRipsis.DefaultValue,0,XronosAnemistiraOn.DefaultValue,XronosAnemistiraOff.DefaultValue,AnamoniAnemistira.DefaultValue,AdraniaAisthitiraFloagas.DefaultValue,StrofesAnafleksis.DefaultValue,AnoOrioAisthitiraFloagas.DefaultValue,KatoOrioAisthitiraFloagas.DefaultValue,EnergopohisiAisthitiriou.DefaultValue,OrioThermokrasiasKausaerion.DefaultValue,OrioThermokrasiasNerou.DefaultValue,KathisterisiAnemistira.DefaultValue,EnergopoihsiEksanemismou.DefaultValue,DiarkiaEksanemismou.DefaultValue,0,StrofesKausisAnemistira.DefaultValue,ThermokrasiaEkkinisisKikloforiti.DefaultValue,AnoOrioThermokrasiasNerou.DefaultValue,KatoOrioThermokrasiasNerou.DefaultValue,AnoOrioThermokrasiasKausaerion.DefaultValue,KatoOrioThermokrasiasKausaerion.DefaultValue};

回答1:


You are trying to initialize the array with values which are unknown at compile time which makes them non-constant.

Also the array contains elements of type int* and you are trying to initialize it with values of type int.

A fix to the first problem would be to initialize each element separately:

DefaultValuesWaterSubMenu[0] = ....;

A fix to the second porblem would be to change the array to contain int values

int DefaultValuesWaterSubMenu[10];

DefaultValuesWaterSubMenu[0] = XronosTrofodosias.DefaultValue;

Or

DefaultValuesWaterSubMenu[0] = &XronosTrofodosias.DefaultValue;


来源:https://stackoverflow.com/questions/48619717/i-get-error-constant-expression-required

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!