This is the mail archive of the
mailing list for the GCC project.
Re: static declaration
- From: Branko Äibej <brane at xbc dot nu>
- To: scott <sirushfe at unity dot ncsu dot edu>
- Cc: gcc at gnu dot org
- Date: Wed, 09 Jul 2003 23:22:34 +0200
- Subject: Re: static declaration
- References: <200307092031.h69KVma16735@uni00mr.unity.ncsu.edu>
> I have been having problem with the use of the static keyword.
>The first problem I have is when I try to create a static class member
>variable. Everything compiles correctly, but upon linking where ever the
>static member variable is used in my code the linker complained about
>there being an undefined reference to the static member variable. I
>thought this might be some other problem, but upon removal of the
>static key word the program compiles and links fine and runs as
You have to define static members, not just declare them.
>The second problem I encounter is when trying to declare
>a class member function (that subtracts one global variable from
>another and returns the differenct) as a static member function. Upon
>compiling the class I get an error saying I cannot declare member
>function 'static int Packet::GetMessageSpace()' to have static linkage.
>If I remove the keyword static from the function declaration in both the
>header file and the c++ file, and change calls to the function from
>Packet::GetMessageSpace() to myPacket.GetMessageSpace()
>(and myPacket is just an object of type Packet)everything compiles
>and links correctly and the program functions correctly.
Static class methods don't have static linkage in C++, so you mustn't
try to define them with static linkage.
> I am running
>RedHat 9 on an intel machine. I would appreciate any help you can
>give me as to the problems I have received, as I expect I have just
>overlooked some obvious step to correct these problems.
What you should do is a) read a book on C++, and b) not ask such
questions on the GCC list. This is not a list about C++ programming,
it's about GCC development.
Brane Äibej <firstname.lastname@example.org> http://www.xbc.nu/brane/