public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Problem with code or gcc?
@ 2005-09-27 13:39 Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA
  2005-09-27 14:12 ` Philippe Haution
  0 siblings, 1 reply; 5+ messages in thread
From: Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA @ 2005-09-27 13:39 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 351 bytes --]

	Could anybody tell me if this is a valid C++ code or
a problem with GCC?

test_prog.cc:
#include <vector>
struct MyStruct {
	static const int CONST = 1;
};

int main() {
	std::vector<int> l;
	MyStruct z;
	l.push_back(z.CONST);
}


	I get an undefined reference to MyStruct::CONST when 
I try to compile it.
	I appreciate any help.

	Artur Kedzierski

[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 4051 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Problem with code or gcc?
  2005-09-27 13:39 Problem with code or gcc? Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA
@ 2005-09-27 14:12 ` Philippe Haution
  0 siblings, 0 replies; 5+ messages in thread
From: Philippe Haution @ 2005-09-27 14:12 UTC (permalink / raw)
  To: Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA, gcc-help

Hello,

This is not a valid C++ code, you cannot define the
value of CONST inside the struct declaration.

A more correct code would be :

struct MyStruct {
 static const int CONST;
};

const int MyStruct::CONST = 1;

Regards,

PH

--- "Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA"
<artur.kedzierski@navy.mil> a écrit :

> 	Could anybody tell me if this is a valid C++ code
> or
> a problem with GCC?
> 
> test_prog.cc:
> #include <vector>
> struct MyStruct {
> 	static const int CONST = 1;
> };
> 
> int main() {
> 	std::vector<int> l;
> 	MyStruct z;
> 	l.push_back(z.CONST);
> }
> 
> 
> 	I get an undefined reference to MyStruct::CONST
> when 
> I try to compile it.
> 	I appreciate any help.
> 
> 	Artur Kedzierski
> 



	

	
		
___________________________________________________________________________ 
Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger 
Téléchargez cette version sur http://fr.messenger.yahoo.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with code or gcc?
  2005-09-27 14:57 Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA
@ 2005-09-27 15:14 ` John Love-Jensen
  0 siblings, 0 replies; 5+ messages in thread
From: John Love-Jensen @ 2005-09-27 15:14 UTC (permalink / raw)
  To: Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA, MSX to GCC

Hi Artur,

> Shouldn't I get a linker error in those other cases?

Not necessarily.

You won't get a linker error if the actual numeric value can be used "as
is", without referencing the variable it is associated with.  Consider it an
optimization of sorts.

If you really dislike this situation[1], you can use enum for your integer
constants in your struct.

[1] I actually PREFER coding like in your example.  But I know quite a few
C++ software engineers that prefer using enums for constants instead --
they'd claim there are "less surprises".  Both idioms will be supported by
C++ for a very long time.

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Problem with code or gcc?
@ 2005-09-27 14:57 Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA
  2005-09-27 15:14 ` John Love-Jensen
  0 siblings, 1 reply; 5+ messages in thread
From: Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA @ 2005-09-27 14:57 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1657 bytes --]

	Philippe and Ryan, thank You for your responses.
	One thing that I find it strange is that I can use
MyStruct::CONST in other ways without a problem:

MyStruct m;
int a = m.CONST
std::vector<int> v;
v.push_back(static_cast<int>(m.CONST));
if (b == m.CONST) 
  ...

	However, only when I use 'v.push_back(m.CONST)', I
get a linker error. Shouldn't I get a linker error in
those other cases?


-----Original Message-----
From: Philippe Haution [mailto:philippe_haution@yahoo.fr]
Sent: Tuesday, September 27, 2005 7:13
To: Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA; gcc-help@gcc.gnu.org
Subject: RE: Problem with code or gcc?


Hello,

This is not a valid C++ code, you cannot define the
value of CONST inside the struct declaration.

A more correct code would be :

struct MyStruct {
 static const int CONST;
};

const int MyStruct::CONST = 1;

Regards,

PH

--- "Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA"
<artur.kedzierski@navy.mil> a écrit :

> 	Could anybody tell me if this is a valid C++ code
> or
> a problem with GCC?
> 
> test_prog.cc:
> #include <vector>
> struct MyStruct {
> 	static const int CONST = 1;
> };
> 
> int main() {
> 	std::vector<int> l;
> 	MyStruct z;
> 	l.push_back(z.CONST);
> }
> 
> 
> 	I get an undefined reference to MyStruct::CONST
> when 
> I try to compile it.
> 	I appreciate any help.
> 
> 	Artur Kedzierski
> 



	

	
		
________________________________________________________________________
___ 
Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo!
Messenger 
Téléchargez cette version sur http://fr.messenger.yahoo.com

[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 4051 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Problem with code or gcc?
@ 2005-09-27 14:12 Ryan Mansfield
  0 siblings, 0 replies; 5+ messages in thread
From: Ryan Mansfield @ 2005-09-27 14:12 UTC (permalink / raw)
  To: Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA, gcc-help

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.11


Regards,
 
Ryan Mansfield
 

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA
Sent: Tuesday, September 27, 2005 9:40 AM
To: gcc-help@gcc.gnu.org
Subject: Problem with code or gcc?

	Could anybody tell me if this is a valid C++ code or
a problem with GCC?

test_prog.cc:
#include <vector>
struct MyStruct {
	static const int CONST = 1;
};

int main() {
	std::vector<int> l;
	MyStruct z;
	l.push_back(z.CONST);
}


	I get an undefined reference to MyStruct::CONST when 
I try to compile it.
	I appreciate any help.

	Artur Kedzierski

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-09-27 15:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-27 13:39 Problem with code or gcc? Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA
2005-09-27 14:12 ` Philippe Haution
2005-09-27 14:12 Ryan Mansfield
2005-09-27 14:57 Kedzierski, Artur CIV NAVSURFWARCENDIV CORONA
2005-09-27 15:14 ` John Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).