Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 6th, 2006, 12:25 AM
Chris Portka
Guest
 
Posts: n/a
Default Use of Static Variables from Outside of Defining Class

How can I modify static variables from outside the class they're
defined in? It doesn't work if I try to access them from a function,
from an instantiated object, or from the class itself. Here's an
example:
MAIN:
#include "test"

int main()
{
Test t;
//All of these give "unresolved external symbol"
t.x = 1;
t.setX(1);
Test::x = 1;
Test::setX(1);
}

TEST:
class Test
{
public:
static int x;
static void setX(int z) { x = z; }
};

  #2  
Old December 6th, 2006, 12:35 AM
Robert Bauck Hamar
Guest
 
Posts: n/a
Default Re: Use of Static Variables from Outside of Defining Class

Chris Portka wrote:
Quote:
How can I modify static variables from outside the class they're
defined in? It doesn't work if I try to access them from a function,
from an instantiated object, or from the class itself. Here's an
example:
MAIN:
#include "test"
>
put
int Test::x;
about here.
Quote:
int main()
{
Test t;
//All of these give "unresolved external symbol"
t.x = 1;
t.setX(1);
Test::x = 1;
Test::setX(1);
}
>
TEST:
class Test
{
public:
static int x;
This is only a declaration. You need a definition too.
Quote:
static void setX(int z) { x = z; }
};
--
Robert Bauck Hamar
Der er to regler for suksess:
1. Fortell aldri alt du vet.
- Roger H. Lincoln
  #3  
Old December 6th, 2006, 01:05 AM
CTG
Guest
 
Posts: n/a
Default Re: Use of Static Variables from Outside of Defining Class

Where is your constructor destructor defined?

Chris Portka wrote:
Quote:
How can I modify static variables from outside the class they're
defined in? It doesn't work if I try to access them from a function,
from an instantiated object, or from the class itself. Here's an
example:
MAIN:
#include "test"
>
int main()
{
Test t;
//All of these give "unresolved external symbol"
t.x = 1;
t.setX(1);
Test::x = 1;
Test::setX(1);
}
>
TEST:
class Test
{
public:
static int x;
static void setX(int z) { x = z; }
};
  #4  
Old December 6th, 2006, 02:05 AM
David Harmon
Guest
 
Posts: n/a
Default Re: Use of Static Variables from Outside of Defining Class

On 5 Dec 2006 16:45:23 -0800 in comp.lang.c++, "Chris Portka"
<chrisportka@gmail.comwrote,
Quote:
> //All of these give "unresolved external symbol"
> t.x = 1;
> t.setX(1);
> Test::x = 1;
This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[10.11] Why are classes with static data members getting linker
errors?" It is always good to check the FAQ before posting. You can
get the FAQ at:
http://www.parashift.com/c++-faq-lite/


 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles