473,378 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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

Aug 24 '08 #1
4 1703
Sam
xi**********@live.com writes:
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;
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.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkixvoQACgkQx9p3GYHlUOIldQCffxXVIeRlSL YtB53H8FS3uCTm
X7MAnjLv7rngqzhGep36hCxUYYC5dKtU
=jbhL
-----END PGP SIGNATURE-----

Aug 24 '08 #2
On Aug 24, 4:03*pm, Sam <s...@email-scan.comwrote:
*application_pgp-signature_part
< 1KViewDownload

xiaoxiaoy...@live.com writes:
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;

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.
Aug 24 '08 #3
On Aug 24, 4:03*pm, Sam <s...@email-scan.comwrote:
*application_pgp-signature_part
< 1KViewDownload

xiaoxiaoy...@live.com writes:
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;

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.
Aug 24 '08 #4
Sam
xi**********@live.com writes:
On Aug 24, 4:03Â*pm, Sam <s...@email-scan.comwrote:
>Â*application_pgp-signature_part
< 1KViewDownload

xiaoxiaoy...@live.com writes:
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;

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?
No. Static initializers can only be initialized by constant expressions.
Whether or not they, themselves, are static, is a different matter.

If you need to initialize a static object with a non-constant expression,
the only way to do this is to make it a class object, that's initialized by
a constructor.
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.
The simplest and most straightforward way to do this, would be, as I said,
by making it a class object:

/* header: */

class A {

public:

A();

int value;
};

extern const A a_value;

/* module */

const A a_value;

A::A()
{
/* Here, write the code to initialize the value member by reading it
from a file */
}
The rest of your application would read the constant a_value.value, that's
initialized by your constructor.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkix0zoACgkQx9p3GYHlUOJ4IgCfWq+mb9T27g 3LjwSzbgZHIR3Z
clsAn3PVYi5XqbXzsyNCmVwY05ORirL8
=+gON
-----END PGP SIGNATURE-----

Aug 24 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Aguilar, James | last post by:
Hey y'all. Suppose I want a member template in a class, but I don't want to include .hh files in a .hh file? For instance: #ifndef _FOO_HH_ #define _FOO_HH_ 1 class Foo { public: Foo()
2
by: Flzw | last post by:
I have a class declared in a header file that has a static vector variable which I get unresolved external errors when I try to access it, I understand static members should be declared in cpp but...
6
by: clinton__bill | last post by:
Hi, I usually use "using namespace <namespace_name>" to reference a namespace. Today I run across a code, in its header file it has this, namespace SP1{ class C1; } While SP1::C1 is a...
2
by: Ken Human | last post by:
I am writing a program that contains multiple source and header files, generally one of each for classes that are similar to each other. I want to be able to declare an object of the class...
1
by: Robby | last post by:
Hello: Can we pass a pointer to an object from one cpp file to another by using a function call. Assuming the object was created and declared along with its accessor methods in the first cpp...
4
by: zfareed | last post by:
#include <iostream> #include <fstream> using namespace std; template<class ItemType> class SortedList { private:
8
by: Johs | last post by:
I see a lot of different conventions on the net when it comes to making a class. Currently I make a class in a single .cc file like: #include <string> #include <iostream> using namespace std; ...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.