473,323 Members | 1,560 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,323 software developers and data experts.

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::testclass(void)
{
}

testclass::~testclass(void)
{
}

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

Jul 22 '05 #1
5 1481

"Leo" <th***********@rogers.com> wrote in message news:uA********************@news04.bloor.is.net.ca ble.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.newshosting.co m...

"Leo" <th***********@rogers.com> wrote in message news:uA********************@news04.bloor.is.net.ca ble.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********@i010.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.newshosting.co m...

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

news:uA********************@news04.bloor.is.net.ca ble.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********@i010.com> wrote in message news:S6********************@news04.bloor.is.net.ca ble.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
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...
29
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...
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
4
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...
7
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...
10
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...
1
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. ...
5
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...
9
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...
13
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.