error C2099: initializer is not a constant


(Paul) #1

Hello all,

I’m working on the ET Dutch project now and instead of replacing all text everywhere i wanted to make a single file. But when i have this:
language.c


const char* Test1 = "CombatErvaring";

and use it here:
bg_misc.c


const char* skillNames[SK_NUM_SKILLS] = {
	Test1,
	"Engineering",
	"First Aid",
	"Signals",
	"Light Weapons",
	"Heavy Weapons",
	"Covert Ops"
};

I get the message:


1>*\src\game\bg_misc.c(204) : error C2099: initializer is not a constant

What am i doing wrong :frowning:


(Wezelkrozum) #2

Im not a coder.
But if I’m right you want to replace Test1 with CombatErvaring?

And btw which line does cause the error? line 204? Is it Test1,? Give us more info.


(Sorlaize) #3

You can’t do that for some reason, even though compilers are heralded as smart :smiley:

The most elegant solution here would be to #define the fields you want to use as they are merely simple strings. If you wanted to you could also map these to an enum fairly simply:

#define E_SKILLS_COMBATERVARING skCombatErvaring
#define E_SKILLS_HEAVYWEAPONS skHeavyWeapons

enum E_SKILLS
{
E_SKILLS_COMBATERVARING,
E_SKILLS_HEAVYWEAPONS
}

const char* skillNames[SK_NUM_SKILLS] = {
#E_SKILLS_COMBATERVARING,
#E_SKILLS_HEAVYWEAPONS
};

Which produces the same as

const char* skillNames[SK_NUM_SKILLS] = {
“skCombatErvaring”,
“skHeavyWeapons”
};

Notice this comes at the limitation of 1 string token, thus “HeavyWeapons” rather than “Heavy Weapons”.

And if you want something funkier maybe try X-Macros.

:slight_smile:


(timestart) #4

Don’t know if this will work on your compiler, but changing

const char* Test1 = "CombatErvaring";

to

const char Test1[] = "CombatErvaring";

works on GCC 4.3.

#include <stdio.h>

const char Test1[] = "CombatErvaring";

const char* skillNames[] = {
  Test1,
  "Engineering",
  "First Aid",
  "Signals",
  "Light Weapons",
  "Heavy Weapons",
  "Covert Ops",
  NULL
};

int main()
{
  int i;

  for (i = 0; skillNames[i] != NULL; ++i) {
    printf("%s
", skillNames[i]);
  }

  return 0;
}


(Paul) #5

I’ts working, many thx!

Just a side-question, is it possible to make 3dimensional arrays?
On php it goes this way:
<?php
$language[‘classes’][‘soldier’] = “Soldaat”;
$language[‘classes’][‘medic’] = “Hospik”;
$language[‘team’][‘allies’] = “Nederland”;
$language[‘team’][‘axis’] = “Duitsland”;

echo $language[‘team’][‘axis’];
?>

Would this be possible in C++

Thx
?>


(nighthitcher) #6

sounds good to me