g++ strange problem

C

Chameleon

With g++ I have this error when I compile:
----------crap.cpp--------------------------------------------------------
struct A {
static const double CRAP = 1;
A() { double a = - CRAP / 2; } // Undefined reference to A::CRAP
A(int) { double a = - (CRAP / 2); }
};

int main() {
A a;
A b(0);
return 0;
}
 
B

Balog Pal

Chameleon said:
With g++ I have this error when I compile:
----------crap.cpp--------------------------------------------------------
struct A {
static const double CRAP = 1;
A() { double a = - CRAP / 2; } // Undefined reference to A::CRAP
A(int) { double a = - (CRAP / 2); }
};

int main() {
A a;
A b(0);
return 0;
}

in-class static const with init is limited to integral types, so you should
get an error right at second line. guess you just skipped over it. If not,
your code is still ill-formed, and you got a diagnostic, even if not the
most helpful one.
 
V

Vaclav Haisman

Chameleon wrote, On 3.4.2011 20:05:
With g++ I have this error when I compile:
----------crap.cpp--------------------------------------------------------
struct A {
static const double CRAP = 1;
Only static const members of integral (int etc.) types can be initialized
inline. Double type is not integral type. This should not even compile.
A() { double a = - CRAP / 2; } // Undefined reference to A::CRAP
A(int) { double a = - (CRAP / 2); }
};

You are missing a definition of the constant member:
const double A::CRAP;
int main() {
A a;
A b(0);
return 0;
}
The standard says so. Static member variables (constant or otherwise) need
out-of-line definition.
It is compiler specific?
No, though some compilers might not issue any error because they optimize any
references to the constant member away.
 
C

Chameleon

Chameleon wrote, On 3.4.2011 20:05:
Only static const members of integral (int etc.) types can be initialized
inline. Double type is not integral type. This should not even compile.

Whow! double is not integral? Why? (long double actually)

You are missing a definition of the constant member:


The standard says so. Static member variables (constant or otherwise) need
out-of-line definition.

No, though some compilers might not issue any error because they optimize any
references to the constant member away.

Yes, g++, has not a problem. Program works just fine. But if it is out
of standard I will change it.

I think that I read on "Thinking in C++" that this code is correct:
 
J

Jonathan Lee

Whow! double is not integral? Why? (long double actually)

Basically, by definition. Integral types are things that are
(roughly speaking) like integers. Double can have fractions,
so that pretty much means they aren't integral types.

As far as C++ is concerned, though, some types are explicitly
stated to be "integral types". Double is not one of those
types.

--Jonathan
 
C

crea

Chameleon said:
I think that I read on "Thinking in C++" that this code is correct:

It seems be allowed only with integrals:
http://en.wikipedia.org/wiki/One_Definition_Rule
"In pre-standard C++, all static data members required a definition outside
of their class. However, during the C++ standardization process it was
decided to lift this requirement for static const integral members." Double
is not an integral...
 
D

Drew Lawson

Basically, by definition. Integral types are things that are
(roughly speaking) like integers. Double can have fractions,
so that pretty much means they aren't integral types.

The clarify what I think may have been the OP's confusion, "integral"
has multiple meanings. Outside of math and computer language
definitions, the more common usage is relating to a part of the
whole. So "integral types" and "built-in types" might look like
the same thing.
 
N

northerntechie

With g++ I have this error when I compile:
----------crap.cpp--------------------------------------------------------
struct A {
     static const double CRAP = 1;
     A() { double a = - CRAP / 2; }    // Undefined referenceto A::CRAP
     A(int) { double a = - (CRAP / 2); }

};

int main() {
     A a;
     A b(0);
     return 0;}

Is not the "undefinded reference to A::CRAP" a result of incorrect
initialization of the "static const" variable?

The static const variable should be only declared within the struct
scope. I believe the static (const or not) should be defined outside
the structure scope as:

static const double A::CRAP = 1; // or 1.0 to be more explicit

Todd S.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top