473,545 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static variable:declar e and define

Hi,
I am confused at static variable in declaring and defining.

I have the following code in header file,

class c0;

class c1 {
static std::vector<c0> cs;
};

And in C file, I operate on c1::cs in some function. But VC7 compiler
complains that c1::cs is not initialized.

I have to add the following line to the top of the C code,
std::vector<c0> c1::cs;
Then it works.

This makes half sense to me. Static variable must be defined in
addition to declaring. But how come the above newly statement does the
definition? Is that because std::vector does some dummy initialization?

Then I have another header file,
class MyString {...};

static MyString myStr; // This variable does not belong to any
class

Then in the C file when I have
myStr = "";
The compiler complains that I am re-defining myStr.

I wonder what is going on? Because myStr is a standalone variable, so
its definition is defferent from class member?

Apr 6 '06 #1
1 1691
li*****@hotmail .com wrote:
I am confused at static variable in declaring and defining.

I have the following code in header file,

class c0;

class c1 {
static std::vector<c0> cs;
That's the declaration.
};

And in C file, I operate on c1::cs in some function. But VC7 compiler
complains that c1::cs is not initialized.

I have to add the following line to the top of the C code,
std::vector<c0> c1::cs;
That's the definition.
Then it works.

This makes half sense to me. Static variable must be defined in
addition to declaring.
Static member variables must. And only if they are of non-integral type
and are used outside the class itself.
But how come the above newly statement does the
definition?
Because it's outside of the class, at the namespace level.
Is that because std::vector does some dummy
initialization?
Nope. It's because the rules require it.
Then I have another header file,
class MyString {...};

static MyString myStr; // This variable does not belong to any
class
Then it's not a _member_, is it?
Then in the C file when I have
myStr = "";
The compiler complains that I am re-defining myStr.
Huh? You must be including the header in more than one translation unit
and that introduces multiple _definitions_ in your program.
I wonder what is going on? Because myStr is a standalone variable, so
its definition is defferent from class member?


Yep. You got it!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 6 '06 #2

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

Similar topics

3
5381
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 static int StaticMethod(){ private static int m = 0; // <<== error 2
2
7513
by: Michelle | last post by:
Hi all, I have a query that scans huge table consists of 8 or more millions records. The funny thing is that if I use the query with local variable, the query takes more than 1 minutes, whereas if I hard code the value into the query, it takes about 1 second. Here are the queries: WITH VARIABLE: -------------------
6
51839
by: Dipak Patel | last post by:
Platform: MS SQL 7.00 - 7.00.1063 Standard Edition / Win2000 I wish to store the numeric result of a query into a variable, as described below. This does not work, it fails on the last line with syntax error. Is there a way to do it? (The reason I want to do this is to store the count into a table, therefore eventually do an update table...
8
2372
by: nilavya | last post by:
Hi Gurus, Since I learned C++, way back in 2002, I always had a doubt about the different types of storage in C++. We have "const", "static" , "#define" for one or other purpose. Now I have a eVC++ application running on WINCE. This application has many classes, some are initialized dynamically and others statically. I have one class which...
2
1432
by: Ishaan | last post by:
Hi, Is it possible to declare a variable dynamically? Anwar -- Software engineer Petroleum Development Oman
8
1817
by: pozz | last post by:
In my software, there are some variables that represents some settings. They usually are numerical variables: unsigned char (0..255), signed char (-127..128), unsigned int (0..65535), signed int (-32767..32768). I want to write a generic C function that changes the value of one parameter. For example, a function that takes a pointer to the...
1
3560
by: kiluyar | last post by:
I have such a function: class T { public: T(){...//some operations} }; T& GetInst()
14
360
by: cs1975 | last post by:
Hi Everyone, I wanted to make a global variable double rowCols = {{1,0,0},{0,1,0},{1,0,0}}; to static global static double rowCols = {{1,0,0},{0,1,0},{1,0,0}}; I am updating this global variable in one of the methods in the flow,
1
1323
by: little83 | last post by:
Dear all I got these error when I declare a variable inside one of the methods Error 47 Use of unassigned local variable 'm' However i don't get these error when I declare it at the top of the class Any ideas
0
7464
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7396
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7751
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.