473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 22 '05 #1
4 2185
MADE A MISTAKE, SORRY, CORRECTED BELOW

Connell Gauld wrote:
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
NOT THIS int exClass::nextFree; BUT THIS

int exClass::exStaticVar

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

Jul 22 '05 #2

"Connell Gauld" <co*****@freebreakfast.co.uk> wrote in message
news:cd**********@newsg2.svr.pol.co.uk...
MADE A MISTAKE, SORRY, CORRECTED BELOW

Connell Gauld wrote:
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


NOT THIS
int exClass::nextFree;

BUT THIS

int exClass::exStaticVar

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


exClass::exStaticVar

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

john
Jul 22 '05 #3
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
Jul 22 '05 #4
Connell Gauld wrote:
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; 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:
exClass * otherClass::doFunction(args)
{
...
if(condition)
{
// The line below works fine.
someFunction(exClass.exStaticVar);

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

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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gernot Frisch | last post by:
Problem: A.cpp: ------ static FOO* gFoo=NULL; A.h extern FOO* gFoo; gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
16
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
10
by: Peter Kirk | last post by:
Hi there I have a question about static variables. There is a function which returns a pointer to a structure "AStructure". The function has a static local variable of type AStructure, and it is...
11
by: Dave | last post by:
I'm trying to understand the implications of using static methods and properties in asp.net so I found an article "Troubleshooting ASP.NET applications with the use of static keywords"...
6
by: Vladislav Kosev | last post by:
I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
2
by: HerbD | last post by:
I have a loooong debugging session behind me! I finally found the reason for the problem and now would like to know, if it is a bug in my code or not standardconformant behavour of the compiler(s) or...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
5
by: Lawrence Krubner | last post by:
Do any problems come up when using a static variable in a cron job? Assuming the cron job is called every 5 minutes for one year. Assume I've got an array that stores the names of which users are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.