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

Structure of Constants

Say I would like my namespace to have some constants associated with
it, like this:
==========
=====
namespace broadpen
{
struct defaults
{
static const double w = 10.0;
static const double r = 0.6435011087932843868;
static const double a = 0.2;

virtual void dummy() = 0;
};
}
=====
==========
however, this example fails with the following errors:
==========
=====
error: ISO C++ forbids initialization of member constant `w' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `r' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `a' of
non-integral type `const double'
=====
==========
this really seems like it should work - is it a fact that I can not
initialize non-integer constants? yikes!!
--
_jsnX

Jul 23 '05 #1
4 2633
jsnX schrieb:
Say I would like my namespace to have some constants associated with
it, like this:
==========
=====
namespace broadpen
{
struct defaults
{
static const double w = 10.0;
static const double r = 0.6435011087932843868;
static const double a = 0.2;

virtual void dummy() = 0;
};
} Your question is about a namespace, but the constants are inside a struct.
=====
==========
however, this example fails with the following errors:
==========
=====
error: ISO C++ forbids initialization of member constant `w' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `r' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `a' of
non-integral type `const double'
=====
==========
this really seems like it should work - is it a fact that I can not
initialize non-integer constants? yikes!!


Not inside a class (or struct) definition. You can move the constants
out of struct defaults and into namespace scope. Or you can declare
them as static const members and define them outside the class
definition like any static member. However that will deprive the
compiler of any chance to optimise by using compile-time constants in
any translation unit other than the one that defines them.

Cheers,
Malte
Jul 23 '05 #2

"jsnX" <ja*********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Say I would like my namespace to have some constants associated with
it, like this:
==========
=====
namespace broadpen
{
struct defaults
{
static const double w = 10.0;
static const double r = 0.6435011087932843868;
static const double a = 0.2;

virtual void dummy() = 0;
};
}
=====
==========
however, this example fails with the following errors:
==========
=====
error: ISO C++ forbids initialization of member constant `w' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `r' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `a' of
non-integral type `const double'
=====
namespace broadpen
{
struct defaults
{
static const double w;
static const double r;
static const double a;

virtual void dummy() = 0;
};
}
and something like this will go into your implementation file:

const double broadpen::defaults::w = 10.0;
const double broadpen::defaults::r = 0.6435011087932843868;
const double broadpen::defaults::a = 0.2;

==========
this really seems like it should work - is it a fact that I can not
initialize non-integer constants? yikes!!


Yes. Not in the class definition.

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Jul 23 '05 #3
would adding 'static' to this code do anything good, performance wise?
or would it just restrict these constants to the file they were in?

namespace broadpen
{
namespace defaults
{
const double w = 10.0;
const double r = 0.6435011087932843868;
const double a = 0.2;
}
}

--
_jsnX

Jul 23 '05 #4
jsnX schrieb:
would adding 'static' to this code do anything good, performance wise?
or would it just restrict these constants to the file they were in?

namespace broadpen
{
namespace defaults
{
const double w = 10.0;
const double r = 0.6435011087932843868;
const double a = 0.2;
}
}


It wouldn't do any good, I'm not even sure if it's allowed inside a
namespace other than the global one (and if so, it's deprecated).
The above code is the way to go for constants that are not of integral type.

Cheers,
Malte
Jul 23 '05 #5

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

Similar topics

27
by: Alberto Vera | last post by:
Hello: I have the next structure: How Can I make it using Python? How Can I update the value of 6?
2
by: Luca | last post by:
I have the following problem: I'm developing a system where there are some processes that communicate each other via message queues; the message one process can send to another process is as...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
7
by: Brian K. Michalk | last post by:
I know I can assign default values to a structure during initialization, but I have a routine that accumulates a lot of floats into an array, and then I would like to assign them to a (reference to...
4
by: Thomas Matthews | last post by:
Hi, I'm writing code for an embedded system. In the system a Timer has 4 memory mapped registers of 32-bit lengths in contiguous locations: Timer 0: 0x1000 Configuration register 0x1004...
6
by: stuie_norris | last post by:
Hi Group, I am trying to define a strucutre of constants. Then switch on this structure of constant values. In the following example of my problem the code fails to compile due to: case...
6
by: Polska, Ursula | last post by:
Hello experts, could explain to initialize a structure? Private Structure sEG Public EN As Integer = 1 Public EG1 As Integer = 2 Public EG9 As String = "EG, 14.2" End Structure Please no...
3
by: Bob Day | last post by:
Using VS 2003, VB.NET, MSDE Is there a way to itterate through the items in a structure? Consider the structure below, which holds the name of wave files. I would like to itterate through them...
5
by: kselvaakumar | last post by:
I have declared a global structure like this: struct { float a; float b; }static one; This is in a separate header file file1.h; Now how can i initialise those variables one.a and one.b in...
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?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.