Question on a class has not declared in a C++ header file.

X

xiaoxiaoyang

Hi,

I have a variable, previously was a constant, and defined in a header
file, e.g., A.h, and then now I would like to change this variable as
can be specified from a parameter file, such as B.cpp. like below:

in A.h before change, I have

static int constant CONST_X = 100;

Now I would like to be specified in B.cpp, that can read the variable
value from a file.

in B.cpp, I have:
static int X = 300; // can read from a file, or change in B.cpp.

I achieve the modification, in A.h, I did the below modification:

static int const CONST_X = class B::X;

or static int const CONST_X = B::X;

But I got error of "B has not been declared" when compiling.

or class B;
static int const CONST_X = B::X.

But I got error of incomplete type "B" used in nested named specified.

I think a stupid method is to comment out the line "static int const
CONST_C=100" in the original A.h, and replaced everywhere in every
class which uses CONST_C with B::X in the code (and define X in B.cpp
as above), but I think this method is so inefficient, and they most be
some method that allow me to only replace the CONST_X by B::X in the
A.h only, like I was trying to do but unsucceeded yet.

Can someone tell me how to solve this problem?

Thank you very much for the help.

Xiaoxiao
 
X

xiaoxiaoyang

 application_pgp-signature_part
< 1KViewDownload





This is an invalid constant initializer. In this context, the value must be
a constant expression. "B::X" is not a constant expression, that is, its
value is not known at compile time.

Thanks a lot for the kind help, Sam. If that is the case, I must take
out of the const in the definiation of
static int const CONST_X = B::X, i.e., change it as static int CONST_X
= B::X, is that right?
If I still want the CONST_X be a constant, because it is a constant
read from a file, can you suggest me what can I do? If I can't do
anything on it, that's fine too.
 
T

Tonni Tielens

Hi,

I have a variable, previously was a constant, and defined in a header
file, e.g., A.h, and then now I would like to change this variable as
can be specified from a parameter file, such as B.cpp. like below:

in A.h before change, I have

static int constant CONST_X = 100;

Now I would like to be specified in B.cpp, that can read the variable
value from a file.

in B.cpp, I have:
static int X = 300; // can read from a file, or change in B.cpp.

I achieve the modification, in A.h, I did the below modification:

static int const CONST_X = class B::X;

or static int const CONST_X = B::X;

But I got error of "B has not been declared" when compiling.

or class B;
static int const CONST_X = B::X.

But I got error of incomplete type "B" used in nested named specified.

I think a stupid method is to comment out the line "static int const
CONST_C=100" in the original A.h, and replaced everywhere in every
class which uses CONST_C with B::X in the code (and define X in B.cpp
as above), but I think this method is so inefficient, and they most be
some method that allow me to only replace the CONST_X by B::X in the
A.h only, like I was trying to do but unsucceeded yet.

Initializing a static variable with another static variable defined in
another file is bad practice anyway, since you cannot tell what the
order of initialization will be. Eg. CONST_X can be initialized before
B::X, meaning CONST_X will have an undefined value. This of course,
will not happen for you at first, but only when you show your
application to your customer.
 
X

xiaoxiaoyang

 application_pgp-signature_part
< 1KViewDownload





This is an invalid constant initializer. In this context, the value must be
a constant expression. "B::X" is not a constant expression, that is, its
value is not known at compile time.

After removing the const, i still got errors.

I just changed the static int const CONST_X = B::X to
static int CONST_X = B::X, but I still got error of "B has not
declared". If i changed it to
static int CONST_X = class B:: X, but I still got error of "expected
primary-expression before "class"".
If I changed it as:
class B;
static int CONST_X = B::X, then i still got error of "incomplete type
'B' used in nested name specifier.

Can anyone suggest what I can do? Thanks.
 
X

xiaoxiaoyang

Initializing a static variable with another static variable defined in
another file is bad practice anyway, since you cannot tell what the
order of initialization will be. Eg. CONST_X can be initialized before
B::X, meaning CONST_X will have an undefined value. This of course,
will not happen for you at first, but only when you show your
application to your customer.- Hide quoted text -

- Show quoted text -

But my customer asks me to allow them change the variable value, i.e.,
read from a file. My B class can read from that file and give that X
value. Previously, the code of const X was defined as const in A.h,
and now the customer wants to change, so that's why i tried this. By
the way, i am new in C++, and the code was written by someone else
before and I need to make the modification according to customer's
need.
Any more help will be appreciated.
 
X

xiaoxiaoyang

(e-mail address removed) kirjutas:







You have to decide if you want CONST_X to be const or not. If it is
const, it can be used by specifying the C-style array sizes and as an
integer template argument, for example. If it is not const, then it
cannot be used this way. This is a hard constraint imposed by the
language so there are no sneak-arounds.

If you are sure you want to make this variable non-const, then you
probably want to make it extern, not static, in order to ensure that all
compilation units see the same value:

// foo.h
extern int CONST_X; // BTW, all-caps is not a good style for a variable

This requires a definition somewhere:

// foo.cpp
int CONST_X = 0;

By some strange reason you want the same variable in class B. In order
to guarantee that the value of the variable is the same, I would suggest
to use a reference in class B. So when one assigns to the reference,
also the global variable changes:

// B.h
class B {
public:
        static int& X;
// ...

};

Static class members also require definition somewhere:

// B.cpp
int& B::X = CONST_X;

Now you can do:

B::X = 42;
std::vector<char> buffer(CONST_X); // buffer of 42 bytes

Anyway, all this stuff remains quite convoluted, especially referring to
the same value from two places, and having a global non-const variable
in the first place.

hth
Paavo- Hide quoted text -

- Show quoted text -

Thanks Sam and Paavo's help. I will try them and let you know whether
that works for my problem after testing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top