> Hi everybody!
> Maybe someone can give me help on this problem?
> Im just writing a class which needs a number of constant numbers and
> strings. So I naivley started off like this:
> header:
> class foo
> {
> const unsigned int a_number=7;
> const unsigned char a_string[] =
> {0x00,0x00,0x00,0x00,0x00,0x00,0x00};
IMHO, you are at this point declaring a class, with class constants. These
constants should therefore be static. They can then be initialized in the
.h file (for most compilers, for some compilers the assignment needs to be
done in the .cpp file). Like you did it (above), it aren't class constants
(because they are not static). Non-static attributes are at the object
level while static attributes are at the class level (in fact it is just
mapping C++ semantic meanings to 'OO' meanings, just like "an interface in
C++ can be created by creating an abstract class").
I would have expected the compiler to give you an error (but I never tried
something like your example), but probably the linker is looking for the
values of the object variables you declared and it cannot find it because
the class is not instantiated yet (so the object variables are not
initialized yet because the class' constructor is not called yet)).
Quote:> public:
> a_function(const unsigned int &, const unsigned char* );
> b_function();
> };
> code:
> ...
> b_function()
> { ...
> a_function(a_number,a_string);
> ...}
I suppose a_function can be called by its users (because it is public) and
that b_function is calling a_function with the class constants as a default
or so. Maybe it is easier to use parameter initialization i.e.
a_function(const unsigned int a_number = 7, const unsigned char* a_string =
{...}) so that users only have to remember a_function, so that users can
see what the defaults are. Remark that users can only override both
parameters or the last parameter (there is no way to indicate to use for
instance a_number = 8 and a_string = the default one), therefore, you could
put the one users will most probably override as the last one. Can be
tricky sometimes. It's only what I guessed from the example however ...
Quote:> this is fine for the compiler (g++) but produces a linker error
> (undefined ref. to a_number, a_string).
> Strange to me is that when I use
> b_function()
> { ...
> unsigned int b_number=a_number;
> a_function(b_number,a_string);
> ...}
> theres no error thrown for a_number but the trick doesnt work for the
> const char*. I tried to move the definition into the constructor
> foo::foo():a_string={...} , ... {
> and chaught some compiler errors, so what is the way its meant to be?
> The only workaround I can think of is using static members (didnt try
> yet) but shouldnt there be another way?
> Thanks in advance for any help / hints!
> Eckardt
> eckardt _at_ asta.rwth-aachen.de