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? 1 1679 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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,...
|
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...
|
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...
|
by: Ishaan |
last post by:
Hi,
Is it possible to declare a variable dynamically?
Anwar
--
Software engineer
Petroleum Development Oman
| |
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...
|
by: kiluyar |
last post by:
I have such a function:
class T
{
public:
T(){...//some operations}
};
T& GetInst()
|
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...
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
| |
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,...
|
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...
|
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...
|
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: 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,...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |