473,385 Members | 1,707 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,385 software developers and data experts.

initializing a constant in the .h file?

hello,
i am attempting to initialize a constant in my .h file. it would be terrific if i
could also assign the constant, but i continue to get compile problems. this
is what i'm trying to do:

(in myfile.h)
const std::string MY_CONSTANT = "permanent value";

but my compiler (gcc version 3.2.1) tells me:

ISO C++ forbids initialization of member

any feedback is appreciated
Jul 22 '05 #1
3 3488
On Sat, 31 Jul 2004 11:29:58 GMT, <an*******@coolgroups.com> wrote:
hello,
i am attempting to initialize a constant in my .h file. it would be
terrific if i
could also assign the constant, but i continue to get compile problems.
this
is what i'm trying to do:

(in myfile.h)
const std::string MY_CONSTANT = "permanent value";

but my compiler (gcc version 3.2.1) tells me:

ISO C++ forbids initialization of member

any feedback is appreciated


Well C++ does indeed forbid in-class initialisation of constants, except
for integral constants.

So you have to initialise your constant outside the class, in a source
file, instead. Is there any reason why this is a problem?

john
Jul 22 '05 #2
On Sat, 31 Jul 2004 12:44:20 +0100, John Harrison
<jo*************@hotmail.com> wrote:
On Sat, 31 Jul 2004 11:29:58 GMT, <an*******@coolgroups.com> wrote:
hello,
i am attempting to initialize a constant in my .h file. it would be
terrific if i
could also assign the constant, but i continue to get compile
problems. this
is what i'm trying to do:

(in myfile.h)
const std::string MY_CONSTANT = "permanent value";

but my compiler (gcc version 3.2.1) tells me:

ISO C++ forbids initialization of member

any feedback is appreciated


Well C++ does indeed forbid in-class initialisation of constants, except
for integral constants.

So you have to initialise your constant outside the class, in a source
file, instead. Is there any reason why this is a problem?

john


I missed that you haven't declared your constant as static, is there any
reason for this?

Normally you would do something like this

// in header file
class MyClass
{
static std::string MY_CONSTANT;
};

// in source file
std::string MyClass::MY_CONSTANT = "permanent value";

john
Jul 22 '05 #3
On 7/31/2004 1:29 PM, an*******@coolgroups.com wrote:
(in myfile.h)
const std::string MY_CONSTANT = "permanent value";


I believe it will help:

http://tinyurl.com/4oanj

Greets

--

Mateusz Łoskot
mateusz at loskot dot net
Jul 22 '05 #4

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

Similar topics

4
by: trying_to_learn | last post by:
I'm learning consts in C++ and the book says that u have to initialize non-static consts inside the constructor initializer list, however "const string* stack" isn't initialized in the constructor...
2
by: Drew McCormack | last post by:
I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I have a header with the following const variables with namespace scope: namespace Periphery { extern const double...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
10
by: Russell Shaw | last post by:
Hi, gcc-3.4 complains about non-integers in: enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"}; even if i cast the strings to integers.
9
by: Christian Christmann | last post by:
Hi, I have problems to initialize a static struct. Here is the meaningful part of the code: int main() { int pA = -100; struct globalMixed4 {
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
2
by: ccfq | last post by:
I need a constant that should not change after initialization. Initialization value is stored in a file. How can i do this? The obvious way is to skip the "const" part and use it freely as a...
2
by: Pavan | last post by:
Hi, I need to create a consant array of larze size but however its elements can be calculated using an equation, say for example I need an int arry of 20 elements where each element will be arr...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.