473,672 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Member Variable?

Leo
I have a member function in this class, which needs a variable to keep
track its status. Thus I used a static member variable.

But I encounter a "unresolved external variable" error during compile
(for the variable a).

Can anyone tell me what I did wrong?
////////////// testclass.h
#pragma once

class testclass
{

public:

// constructor and destructor
testclass(void) ;
~testclass(void );

// static part
static int a;
static void test(void);

};


////////////// testclass.cpp

#include "testclass. h"

testclass::test class(void)
{
}

testclass::~tes tclass(void)
{
}

void testclass::test (void)
{
// problem appears after I put this line
a=1;
}

Jul 22 '05 #1
5 1493

"Leo" <th***********@ rogers.com> wrote in message news:uA******** ************@ne ws04.bloor.is.n et.cable.rogers .com...
But I encounter a "unresolved external variable" error during compile
(for the variable a).


You don't define the variable testclass::a anywhere. You get away with it
as long as nobody uses it.

in testclass.cpp (after the #include)
put
int testclass::a;

You can give it an initializer if the default (zero) is not appropriate.

Jul 22 '05 #2
But how come this is not necessary for non-static members? (that we just
need to *declare* them in the header file);

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

"Leo" <th***********@ rogers.com> wrote in message news:uA******** ************@ne ws04.bloor.is.n et.cable.rogers .com...
But I encounter a "unresolved external variable" error during compile
(for the variable a).


You don't define the variable testclass::a anywhere. You get away with it
as long as nobody uses it.

in testclass.cpp (after the #include)
put
int testclass::a;

You can give it an initializer if the default (zero) is not appropriate.

Jul 22 '05 #3
"Calvin Lai" <ca********@i01 0.com> wrote...
But how come this is not necessary for non-static members? (that we just
need to *declare* them in the header file);
Because you "define" them when you define an instance of the class.
They are created along with an object of the class. Heck, an object
of the class _consists_ of them, without them it wouldn't be.

Please don't top-post.

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

"Leo" <th***********@ rogers.com> wrote in message

news:uA******** ************@ne ws04.bloor.is.n et.cable.rogers .com...
But I encounter a "unresolved external variable" error during compile
(for the variable a).


You don't define the variable testclass::a anywhere. You get away with it as long as nobody uses it.

in testclass.cpp (after the #include)
put
int testclass::a;

You can give it an initializer if the default (zero) is not appropriate.


Jul 22 '05 #4
Calvin Lai wrote:
But how come this is not necessary for non-static members? (that we just
need to *declare* them in the header file);


"Define" is being used here to mean "allocate storage." Storage for
non-static member variables is provided as part of each instance of the
class. Since static member variables by definition do not correspond to
any instance, you have to state explicitly where they should be stored.

Jul 22 '05 #5

"Calvin Lai" <ca********@i01 0.com> wrote in message news:S6******** ************@ne ws04.bloor.is.n et.cable.rogers .com...
But how come this is not necessary for non-static members? (that we just
need to *declare* them in the header file);


Because they are created with each object as it is created (it's part of the object).
The non-static members have to be put some where exactly once.

Jul 22 '05 #6

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

Similar topics

3
5387
by: IHateSuperman | last post by:
public class StaticField2{ public static void main(String args){ private int x, y; // <<== error 1 for ( y = 0 ; y < 100 ; y++){ x = StaticMethod(); System.out.println(" x = "+x); } } public static int StaticMethod(){ private static int m = 0; // <<== error 2
29
4391
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
16
2973
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
4
4890
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors that the static member variable is unknown (unresolved external symbol) Below is what an example of the code I use. Can somebody tell me what I am doing wrong ? Thanks very much in advance, Serge //myclass.h
7
12450
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this data will be initialized if it is used across many translation unit, assume that it has constructor and needs dynamic initialization? I have blindly searched for the answer but I still not thoroughly
10
2902
by: Rene | last post by:
I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile: public class SomeClass { public SomeClass() { abc++; // Instance Variable xyz++; // Static Variable
1
3159
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. And I haved used the sizeof operator to test a class which has a static data member, the class size is all the nonstatic data member except the static data member. In compiler's view, is the static data member constructed after the whole class...
5
11318
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can always optimize and turn it into a static const variable to save runtim memory/stack space since being const it cannot be changed. (?) Thanks,
9
8884
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've *defined* "x"'s value to be 10, isn't above statement a
13
2300
by: yanlinlin82 | last post by:
I'd like to write all code of a class in only header file. But when there is any static member varia ble, I have to define it in cpp file, not the header file. Otherwise, the static member variable may be duplicated because two or more cpp files may include the header. How can I solve this without the cpp file, which used to define the static member variable? Thanks!
0
8404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8931
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8828
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8680
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4418
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.