Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > g++ strange problem

Reply
Thread Tools

g++ strange problem

 
 
Chameleon
Guest
Posts: n/a
 
      04-03-2011
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;
}
--------------------------------------------------------------------------
Why?
It is compiler specific?
 
Reply With Quote
 
 
 
 
Balog Pal
Guest
Posts: n/a
 
      04-03-2011

"Chameleon" <(E-Mail Removed)>
> 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;
> }
> --------------------------------------------------------------------------
> Why?
> It is compiler specific?


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.

 
Reply With Quote
 
 
 
 
Vaclav Haisman
Guest
Posts: n/a
 
      04-03-2011
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;
> }
> --------------------------------------------------------------------------
> Why?

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.

--
VH
 
Reply With Quote
 
Chameleon
Guest
Posts: n/a
 
      04-04-2011
> 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.


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


>> 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;
>> }
>> --------------------------------------------------------------------------
>> Why?

> 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.


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:
-----
class A {
static const int AAAA = 1;
};
-----
so, the static const double AAAA = 1.0; is very different?
 
Reply With Quote
 
Jonathan Lee
Guest
Posts: n/a
 
      04-04-2011
On Apr 3, 7:14*pm, Chameleon <(E-Mail Removed)> wrote:
> > 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.

>
> 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
 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      04-04-2011

"Chameleon" <(E-Mail Removed)> wrote in message
news:inb2hb$qd9$(E-Mail Removed)...
>
> I think that I read on "Thinking in C++" that this code is correct:
> -----
> class A {
> static const int AAAA = 1;
> };
> -----
> so, the static const double AAAA = 1.0; is very different?
>


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...


 
Reply With Quote
 
Drew Lawson
Guest
Posts: n/a
 
      04-04-2011
In article <(E-Mail Removed)>
Jonathan Lee <(E-Mail Removed)> writes:
>On Apr 3, 7:14*pm, Chameleon <(E-Mail Removed)> wrote:
>> > 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.

>>
>> 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.


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.


--
Drew Lawson | What is an "Oprah"?
| -- Teal'c
|
 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      04-04-2011
On Mon, 04 Apr 2011 03:14:34 +0300, Chameleon <(E-Mail Removed)>
wrote:

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


Double is a floating point type. Integral types are integers.
 
Reply With Quote
 
northerntechie
Guest
Posts: n/a
 
      04-05-2011
On Apr 3, 11:05*am, Chameleon <(E-Mail Removed)> wrote:
> 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;}
>
> --------------------------------------------------------------------------
> Why?
> It is compiler specific?


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AVG Email Scanner activating at strange times with strange IP addresses dennispublic@hotmail.com Computer Support 1 08-26-2006 04:27 AM
strange problem John Wireless Networking 4 02-14-2005 05:40 PM
Problem installing modules on redhat 9 - strange connection problem championsleeper Perl Misc 0 01-26-2005 12:19 PM
Strange error! Compiler problem? Or code problem?!!!!!!! Pedro Miguel Carvalho C++ 2 10-25-2004 02:06 PM
Question About Strange 'C' Code Syntax ( Well strange to me anyway ) Harvey Twyman C Programming 8 10-25-2003 05:54 AM



Advertisments