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

static data member lost?

I have a class that has a public data member.
If however I declare that data member as being static, then the linker
fails and says the symbol is undefined.
Must static data members be private?

Jul 22 '05 #1
5 1292
JustSomeGuy wrote:
I have a class that has a public data member.
If however I declare that data member as being static, then the linker
fails and says the symbol is undefined.
Must static data members be private?


No. But static data members have to be _defined_ at the namespace level.

V
Jul 22 '05 #2
Victor Bazarov wrote:
JustSomeGuy wrote:
I have a class that has a public data member.
If however I declare that data member as being static, then the linker
fails and says the symbol is undefined.
Must static data members be private?


No. But static data members have to be _defined_ at the namespace level.

V


Ok I think I see...

so if I have a class
class myclass
{
public:
static int x;
};

main()
{
myclass c;
cout << std::c.x;
}
Is that correct?

Jul 22 '05 #3
JustSomeGuy wrote:
Victor Bazarov wrote:

JustSomeGuy wrote:
I have a class that has a public data member.
If however I declare that data member as being static, then the linker
fails and says the symbol is undefined.
Must static data members be private?


No. But static data members have to be _defined_ at the namespace level.

V

Ok I think I see...

so if I have a class
class myclass
{
public:
static int x;
};

main()
{
myclass c;
cout << std::c.x;
}
Is that correct?


No. First of all, myclass::x is still not defined anywhere. Second,
what's "std::c.x"? Why is 'c' prefixed with 'std::'? Your object is
not in the 'std' namespace, is it? Third, 'cout' is undefined. Did
you forget to include <iostream>, maybe? Fourth, 'main' is a function,
every function needs a return value type: "int main()".

To define myclass::x you need to add this line:

int myclass::x;

somewhere in the same namespace where 'myclass' is defined.

V
Jul 22 '05 #4
In message <cl**********@news.u-bordeaux.fr>, Kante Mamadou Moustapha
<mk****@emi.u-bordeaux.fr> writes
Mike Wahler a écrit :
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:41***************@ucalgary.ca...
Victor Bazarov wrote:
JustSomeGuy wrote:

>I have a class that has a public data member.
>If however I declare that data member as being static, then the linker
>fails and says the symbol is undefined.
>Must static data members be private?

No. But static data members have to be _defined_ at the namespace level.
V

Ok I think I see...

No, I don't think you do. See below.
so if I have a class
class myclass
{
public:
static int x;
};

main()
{
myclass c;
cout << std::c.x;
}
Is that correct?

No. Did you even try to compile that? It contains several errors.
#include <iostream>
class myclass
{
public:
static int x;
};
int myclass::x;
int main()
{
std::cout << myclass::x << '\n'; /* prints 0 */
return 0;
}
-Mike

and he should initialize it before using,


No need. Since it's static, it gets initialised.
yes you can say compiler do it, but not sure,
You may not be sure, but the standard is: "The storage for objects with
static storage duration (3.7.1) shall be zero-initialized (8.5) before
any other initialization takes place." (3.6.2/1)
normalization say nothing about that, it depends on the compiler.


???

--
Richard Herring
Jul 22 '05 #5

"Kante Mamadou Moustapha" <mk****@emi.u-bordeaux.fr> wrote in message
news:cl**********@news.u-bordeaux.fr...
Mike Wahler a écrit :
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:41***************@ucalgary.ca...
Victor Bazarov wrote:
JustSomeGuy wrote:

>I have a class that has a public data member.
>If however I declare that data member as being static, then the linker
>fails and says the symbol is undefined.
>Must static data members be private?

No. But static data members have to be _defined_ at the namespace
level.
V

Ok I think I see...

No, I don't think you do. See below.

so if I have a class
class myclass
{
public:
static int x;
};

main()
{
myclass c;
cout << std::c.x;
}
Is that correct?

No. Did you even try to compile that? It contains several errors.
#include <iostream>

class myclass
{
public:
static int x;
};

int myclass::x;

int main()
{
std::cout << myclass::x << '\n'; /* prints 0 */
return 0;
}

-Mike



and he should initialize it before using,


Only if an initial value of zero is not acceptable.
yes you can say compiler do
it,
Yes, the language standard requires that it be initialized to
zero.
but not sure, normalization say nothing about that,
The ISO standard does, in no uncertain terms.
it depends on
the compiler.


Yes, it depends upon whether the compiler's behavior
conforms to the standard. If it does, the object will
be zero-initialized. Always.

-Mike
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
8
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
4
by: sandeep | last post by:
Hi why we cannot have static as a structure member? & also is there any way to achive data hiding in C at this level( i.e. access only selected structure member ) following code gives syntax...
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. ...
10
by: shanknbake | last post by:
I'm getting the following compile-time error: error C2352: 'Person::getCount' : illegal call of non-static member function Here is my getCount function declaration:...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.