Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

static member of class 1

Status
Not open for further replies.

sandup

Technical User
Jun 7, 2003
24
0
0
RO
Hi,
I'm doing this according to Straustrup but VC gives the error:
unresolved external symbol "public: static int something::N"

What's going on?

struct something {
int x;
static int N;
};

void main()
{
something::N = 0;
}
 
// initialize static member variable
int something::N = 0;

void main()
{
}

-pete
 
Thanks!

I knew I was forgetting something.

Just as a remark - why does it need to be defined as int again?
 
yes, int again and without static again.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
> why does it need to be defined as int again?

Interesting question. I think only mr Stroustrup knows the answer.

/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
>why does it need to be defined as int again?
it is not defined again, it is initialized. You must initialize static members outside class declaration and outside .h files.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Yesyes, but the question is I think why do you have to write it
int something::N = 0;
when one would think
something::N = 0;
technically could be enough.



/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
maybe it is remnants from old c when you had to say &quot;struct mystruct&quot; when using them?

Matt
 
I mailed mr Stroustrup himself, and got this reply:
-----
Consistency of the grammar, redundancy to allow checking, readability.

You could equivalently ask &quot;why restate the type of a function when we have
already seen a declaration?:

int f(int a, char c);
//...
f { /* ... */ } // why not?
-----


/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
Since i can't give Bjarne the star i will give it to you for the effort! [lol]

-pete
 
I did not know Mr Stroustrup took the time to answer (lame) questions like my own :)) lol

Actually he's got a point there, bit where do you draw the line? If you keep going in the same direction, you'd end up declaring a variable every time you use it... :/

To Ion Filipski -- multumesc Ioane! ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top