Connecting Tech Pros Worldwide Help | Site Map

Static Variable Problems

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:45 PM
Connell Gauld
Guest
 
Posts: n/a
Default Static Variable Problems

Hi,
I'm using VC++.
I have a class called exClass defined in exClass.h
exClass has a static variable, declared like this in the public section.
static int exStaticVar;
Below the class definition I have initialised (right word?) it like this
int exClass::nextFree;

Now below is a code fragment from a non-static function in another class
(exClass.h was included at the top of the .cpp file).

exClass * otherClass::doFunction(args)
{
if (condition)
{
for(loop_stuff)
{
if(condition)
{
// The line below works fine.
someFunction(exClass.exStaticVar);

// The line below does not.
exClass.exStaticVar++;
}

return gModel;
}
} else {
return 0;
}
}

The error caused is below and suggests that it cannot find exClass which
it must be able to do since it did it in the function.
error C2143: syntax error : missing ';' before '.'

Using VC++.

Any help greatly appreciated,
Connell

  #2  
Old July 22nd, 2005, 04:45 PM
Connell Gauld
Guest
 
Posts: n/a
Default Re: Static Variable Problems

MADE A MISTAKE, SORRY, CORRECTED BELOW

Connell Gauld wrote:
[color=blue]
> Hi,
> I'm using VC++.
> I have a class called exClass defined in exClass.h
> exClass has a static variable, declared like this in the public section.
> static int exStaticVar;
> Below the class definition I have initialised (right word?) it like this[/color]

NOT THIS[color=blue]
> int exClass::nextFree;[/color]
BUT THIS

int exClass::exStaticVar
[color=blue]
>
> Now below is a code fragment from a non-static function in another class
> (exClass.h was included at the top of the .cpp file).
>
> exClass * otherClass::doFunction(args)
> {
> if (condition)
> {
> for(loop_stuff)
> {
> if(condition)
> {
> // The line below works fine.
> someFunction(exClass.exStaticVar);
>
> // The line below does not.
> exClass.exStaticVar++;
> }
>
> return gModel;
> }
> } else {
> return 0;
> }
> }
>
> The error caused is below and suggests that it cannot find exClass which
> it must be able to do since it did it in the function.
> error C2143: syntax error : missing ';' before '.'
>
> Using VC++.
>
> Any help greatly appreciated,
> Connell[/color]
  #3  
Old July 22nd, 2005, 04:45 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Static Variable Problems


"Connell Gauld" <connell@freebreakfast.co.uk> wrote in message
news:cdoc6a$870$1@newsg2.svr.pol.co.uk...[color=blue]
> MADE A MISTAKE, SORRY, CORRECTED BELOW
>
> Connell Gauld wrote:
>[color=green]
> > Hi,
> > I'm using VC++.
> > I have a class called exClass defined in exClass.h
> > exClass has a static variable, declared like this in the public section.
> > static int exStaticVar;
> > Below the class definition I have initialised (right word?) it like this[/color]
>
> NOT THIS[color=green]
> > int exClass::nextFree;[/color]
> BUT THIS
>
> int exClass::exStaticVar
>[color=green]
> >
> > Now below is a code fragment from a non-static function in another class
> > (exClass.h was included at the top of the .cpp file).
> >
> > exClass * otherClass::doFunction(args)
> > {
> > if (condition)
> > {
> > for(loop_stuff)
> > {
> > if(condition)
> > {
> > // The line below works fine.
> > someFunction(exClass.exStaticVar);
> >
> > // The line below does not.
> > exClass.exStaticVar++;
> > }
> >
> > return gModel;
> > }
> > } else {
> > return 0;
> > }
> > }
> >
> > The error caused is below and suggests that it cannot find exClass which
> > it must be able to do since it did it in the function.
> > error C2143: syntax error : missing ';' before '.'
> >
> > Using VC++.
> >
> > Any help greatly appreciated,
> > Connell[/color][/color]

exClass::exStaticVar

in both places. exClass.exStaticVar is wrong both times.

john


  #4  
Old July 22nd, 2005, 04:45 PM
JKop
Guest
 
Posts: n/a
Default Re: Static Variable Problems

Header file:

Class Blah
{
public:
static WhateverClass super_blah;
};


SOURCE file:

WhateverClass Blah::super_blah(87,234,21.3);


If you stick the definition into the header file, then
you'll have a mutliple definition if more than one source
file includes it.

Rule of thumb: If you've got a static member, you're going
to need a source file, even if it's only going to be 1 line
long.


-JKop
  #5  
Old July 22nd, 2005, 05:17 PM
Gottfried Eibner
Guest
 
Posts: n/a
Default Re: Static Variable Problems

Connell Gauld wrote:
[color=blue]
> Hi,
> I'm using VC++.
> I have a class called exClass defined in exClass.h
> exClass has a static variable, declared like this in the public section.
> static int exStaticVar;
> Below the class definition I have initialised (right word?) it like this
> int exClass::nextFree;[/color]
No,

class exClass { static int staticVar; ... } // is reffered as definition
(header file)
int exClass::staticVar; // is reffered as declaration or instantiation
(implementation file)
int exClass::staticVar=0;// declared and initialised at the same time!

To your problem in the code:
[color=blue]
> exClass * otherClass::doFunction(args)
> {
> ...
> if(condition)
> {
> // The line below works fine.
> someFunction(exClass.exStaticVar);
>
> // The line below does not.
> exClass.exStaticVar++;
> }
> ...
> }[/color]
If you use exClass.*, you mean a instance of class exClass with the name
exClass, i.e. like:
int nameOfInstance; // int is the type and nameOfInstance the name of an
instance of type int;
exClass exClass; // the first exClass is the type and the second
would be the instance.

So, don't mix up instance names and class names. If you want to use the
static variable of a class, you have to refere to it via its namespace:
AnyClass::staticVariableName.
In this way you can access the variable anywhere in your code.
If you use it in class-function, you can leave the AnyClass:: referring
scheme.

So in your code just use staticVar (since you use it in the implementation
part of your class).

You can also access static vars of an class via an instance:

exClass instanceOfExClass;
instanceOfExClass.staticVar =0;

But keep in mind, that it is a static variable and any change of staticVar
is visible to all instances.
Static functions and variables do not need any instance of the class (by the
way).

So use exClass::staticVar of just staticVar in your code.

I do not know your definition of someFunction and why it works in the first
place. But it's strange code anyway.

regards,
Gottfried
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.