473,404 Members | 2,170 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,404 software developers and data experts.

static member variable

given the following code:

myclass.h:
-------------------------------

class CMyClass
{
public:
CMyClass();
private:
static int test;
}
--------------------------------
myclass.cpp:
-------------------------------

#include "myclass.h"

CMyClass::CMyClass
{
CMyClass::test = 5; // error: unresolved external symbol
}
--------------------------------
I want to make a private member variable of static kind. What did I make
wrong? I always get the error message "unresolved external symbol".
I've already tried to access it via "this->test" but this doesn't work
either. Is it not allowed to access the static variable outside the same
file? What do I have to do in order to be able to access it from within my
member-functions?

any help is appreciated,
ekim
Jul 22 '05 #1
3 1694
myclass.cpp should contain a line:

int CMyClass::test = 5; // initial value for the static variable must be set
separately

It's ok to use the variable in the constructor (or any method) if you need
this.

Make sure you understand the difference between link & compile time errors
for the future.

HTH, Iakov

"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
given the following code:

myclass.h:
-------------------------------

class CMyClass
{
public:
CMyClass();
private:
static int test;
}
--------------------------------
myclass.cpp:
-------------------------------

#include "myclass.h"

CMyClass::CMyClass
{
CMyClass::test = 5; // error: unresolved external symbol }
--------------------------------
I want to make a private member variable of static kind. What did I make
wrong? I always get the error message "unresolved external symbol".
I've already tried to access it via "this->test" but this doesn't work
either. Is it not allowed to access the static variable outside the same
file? What do I have to do in order to be able to access it from within my
member-functions?

any help is appreciated,
ekim

Jul 22 '05 #2
Hi Ekim:

You have declare a static member variable and use it,
but you haven't defined it.

In your source put the definition after #include "myclass.h"
int CMyClass::test=0; //for example

Ekim escribió:
given the following code:

myclass.h:
-------------------------------

class CMyClass
{
public:
CMyClass();
private:
static int test;
}
--------------------------------
myclass.cpp:
-------------------------------

#include "myclass.h"

CMyClass::CMyClass
{
CMyClass::test = 5; // error: unresolved external symbol
}
--------------------------------
I want to make a private member variable of static kind. What did I make
wrong? I always get the error message "unresolved external symbol".
I've already tried to access it via "this->test" but this doesn't work
either. Is it not allowed to access the static variable outside the same
file? What do I have to do in order to be able to access it from within my
member-functions?

any help is appreciated,
ekim

Jul 22 '05 #3
problem solved - thx a lot to both of you!

by ekim

"Iakov Nakhimovski" <ia****************@hotmail.com> wrote in message
news:cg**********@news.island.liu.se...
myclass.cpp should contain a line:

int CMyClass::test = 5; // initial value for the static variable must be set separately

It's ok to use the variable in the constructor (or any method) if you need
this.

Make sure you understand the difference between link & compile time errors
for the future.

HTH, Iakov

"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
given the following code:

myclass.h:
-------------------------------

class CMyClass
{
public:
CMyClass();
private:
static int test;
}
--------------------------------
myclass.cpp:
-------------------------------

#include "myclass.h"

CMyClass::CMyClass
{
CMyClass::test = 5; // error: unresolved external

symbol
}
--------------------------------
I want to make a private member variable of static kind. What did I make
wrong? I always get the error message "unresolved external symbol".
I've already tried to access it via "this->test" but this doesn't work
either. Is it not allowed to access the static variable outside the same
file? What do I have to do in order to be able to access it from within my member-functions?

any help is appreciated,
ekim


Jul 22 '05 #4

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.