[Qt-interest] Error in defining a static data member to a class

Constantin Makshin cmakshin at gmail.com
Thu Oct 29 22:33:08 CET 2009


Yes, you're right. I confused static variables with static constants.  
Integer constants can be defined in the class declaration, but variables  
have to be initialized in a .cpp file.

On Fri, 30 Oct 2009 00:19:57 +0300, Jason H <scorp1us at yahoo.com> wrote:
> Static member variables have to be allocated in a cpp, so that there is  
> only one copy. The class ceclaration is availible to all files, the  
> definition is only avavilible to one file. This same file should also  
> declare the allocation for static member variables.
>
> This is true on your platform as it is on mine (MSVC)
> ________________________________
> From: Akshey Jawa <aksheyjawa at gmail.com>
> To: Constantin Makshin <dinosaur-rus at users.sourceforge.net>
> Cc: Qt Interest <qt-interest at trolltech.com>
> Sent: Thu, October 29, 2009 12:36:10 PM
> Subject: Re: [Qt-interest] Error in defining a static data member to a  
> class
>
> Thanks a lot. The in-class definition gave a compilation error. But the  
> defining in .cpp file worked!
> Is it some problem with Qt or the same applies to other compilers too ?  
> I am not an experienced programmer.
>
> Thanks,
> Akshey
>
>
> On Thu, Oct 29, 2009 at 1:36 AM, Constantin Makshin  
> <dinosaur-rus at users.sourceforge.net> wrote:
>
> Either define it in the class declaraion
>>
>>> class ClassName
>>> {
>>>     static int temp = 0;
>>> };
>>
>>> or in one of your .cpp files
>>
>>> // someclass.h
>>> class ClassName
>>> {
>>>     static int temp;
>>> };
>>
>>> // someclass.cpp
>>> #include "someclass.h"
>>> int ClassName::temp = 0;
>>
>>> On Wed, 28 Oct 2009 14:21:46 +0300, Akshey Jawa <aksheyjawa at gmail.com>
>>> wrote:
>>
>>> Hi,
>>>>
>>>> I am getting an error in defining a static data member to a class. The
>>>> declaration of the static member does not give any error but when I
>>>> define the variable then I get an the error. Example--
>>>>
>>>> I got error for this statement-
>>>> *int ClassName::temp=0;*
>>>>
>>>> *Error:*  multiple definition of `ClassName::temp'.
>>>> first defined here:
>>>> main.o:/usr/include/c++/4.2/bits/locale_facets.tcc:2560
>>>>
>>>> I have tried changing the names of the identifiers and also tried
>>>> introducing static data members in some other working code, but I get
>>>> similar errors. I am using Qt Creator but I get same error on command
>>>> line also.
>>>>
>>>> Please help
>>>>
>>>> Regards,
>>>> Akshey
>>
>> --
>>> Constantin Makshin

-- 
Constantin Makshin



More information about the Qt-interest-old mailing list