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

Constantin Makshin cmakshin at gmail.com
Thu Oct 29 22:18:50 CET 2009


It's a basic C++ question not related to any toolkit.

On Thu, 29 Oct 2009 19:36:10 +0300, Akshey Jawa <aksheyjawa at gmail.com>  
wrote:
> 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