Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static member variable

Reply
Thread Tools

static member variable

 
 
Carlos Martinez Garcia
Guest
Posts: n/a
 
      08-24-2004
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
>
>

 
Reply With Quote
 
 
 
 
Iakov Nakhimovski
Guest
Posts: n/a
 
      08-24-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Ekim
Guest
Posts: n/a
 
      08-24-2004
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


 
Reply With Quote
 
Ekim
Guest
Posts: n/a
 
      08-24-2004
problem solved - thx a lot to both of you!

by ekim

"Iakov Nakhimovski" <(E-Mail Removed)> wrote in message
news:cgeu88$2j6$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
Does a static variable in a class's member fn always remain static? Sam C++ 4 01-13-2004 11:05 PM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM



Advertisments