Connecting Tech Pros Worldwide Forums | Help | Site Map

Static Variable Problems

Connell Gauld
Guest
 
Posts: n/a
#1: Jul 22 '05
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

Connell Gauld
Guest
 
Posts: n/a
#2: Jul 22 '05

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]
John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

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


JKop
Guest
 
Posts: n/a
#4: Jul 22 '05

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
Gottfried Eibner
Guest
 
Posts: n/a
#5: Jul 22 '05

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
Closed Thread