Hi,
I have a class with some static variables:
in MyClass.h
//-------------------------
lass MyClass{
....
public:
static long myVals[4]; // declaration
}
long MyClass:: myVals[4] = {0,0,0,0}; //definition
//----------------------------
Then in another part of my program: (in This.cpp)
MyClass:: myVals[1] = 123;
But then later on when I try to retrieve this: (in That.cpp)
long x = MyClass:: myVals[1] ;
x always equals zero!?
Everything compiles with no errors or warnings, so can anyone point out
why the static is losing its value?
Thanks
Steve 13 1801
* Steve Edwards: Hi,
I have a class with some static variables:
in MyClass.h //------------------------- lass MyClass{
Just tell me where to send my contribution, and I'll support the
movement to make "lass" a C++ keyword.
... public: static long myVals[4]; // declaration } long MyClass:: myVals[4] = {0,0,0,0}; //definition
This is the beauty of the 'lass' keyword: you don't need semicolons
where you'd otherwise need them using 'class'.
//----------------------------
Then in another part of my program: (in This.cpp)
MyClass:: myVals[1] = 123;
But then later on when I try to retrieve this: (in That.cpp)
long x = MyClass:: myVals[1] ;
x always equals zero!?
That's a Great Mysterie.
Everything compiles with no errors or warnings, so can anyone point out why the static is losing its value?
Obviously, it's the lass pulling your strings. Or, take a look at line
666. Now, that's an ominous number, so that's surely where the problem
is located.
If that doesn't help, then try to post a minimal program that exhibits
the problem.
Hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
OK, I probably asked for that (...even though "no errors or warnings"
would suggest it was a newsgroup-only typo.)
class MyClass{
....
public:
static long myVals[4]; // declaration
};
long MyClass:: myVals[4] = {0,0,0,0}; //definition
Do I take it from the "Mysterie...666 " comment that there shouldn't be
a problem and so I must have a mistake elsewhere?
Thanks
Steve
Steve555 <fo*******@btin ternet.com> wrote: OK, I probably asked for that (...even though "no errors or warnings" would suggest it was a newsgroup-only typo.)
class MyClass{ ... public: static long myVals[4]; // declaration }; long MyClass:: myVals[4] = {0,0,0,0}; //definition
Do I take it from the "Mysterie...666 " comment that there shouldn't be a problem and so I must have a mistake elsewhere?
Well, the problem is, that the code you gave:
Then in another part of my program: (in This.cpp)
MyClass:: myVals[1] = 123;
But then later on when I try to retrieve this: (in That.cpp)
long x = MyClass:: myVals[1] ;
x always equals zero!?
does not show anything about your real setup. Pasting the above
(without obvious text) into a cpp will not compile, thus making us guess
what your code looks like.
By 'post a minimal program that exhibits the problem' Alf told you
to post the *tiniest* possible source (including #include's, main, class
definitions) that still shows the problem. This excludes any defintions,
functions and whatnot, that do not influence the problem.
hth
--
jb
(reply address in rot13, unscramble first)
Apologies, hopefully this will clarify my problem:
----- In Test.h: -----
class Test
{
public:
Test(){};
virtual ~Test(){};
static long myVal;
};
long Test::myVal;
----- In main.cpp: -----
#include "Test.h"
#include "GetVal.h"
int main(int argc, const char *argv[])
{
Test::myVal = 17;
GetVal();
}
----- In GetVal.cpp -----
#include "Test.h"
#include "GetVal.h"
void GetVal()
{
long x = Test::myVal;
}
----- In GetVal.h -----
void GetVal();
The problem is that when GetVal() is called, x =0
In case it isn't obvious, I'm trying to create a 'global' type of
variable called myVal as part of the class Test, which maintains its
value, wherever it is accessed.
(I'm porting some old C code and trying to tidy globals in to statics.)
Thanks
Steve
Steve555 <fo*******@btin ternet.com> wrote: Apologies, hopefully this will clarify my problem:
----- In Test.h: ----- class Test { public: Test(){}; virtual ~Test(){}; static long myVal; }; long Test::myVal;
The above line should go into one compilation unit only. Actually, I
wonder why you did not get a linker error when linking main.cpp and
GetVal.cpp, because they both include Test.h and thus also both define
Test::myVal. ----- In main.cpp: ----- #include "Test.h" #include "GetVal.h" int main(int argc, const char *argv[]) { Test::myVal = 17; GetVal(); }
----- In GetVal.cpp ----- #include "Test.h" #include "GetVal.h"
You could insert it here, for example:
long Test::myVal;
void GetVal() { long x = Test::myVal; }
----- In GetVal.h ----- void GetVal();
The problem is that when GetVal() is called, x =0
Should be solved now.
regards
--
jb
(reply address in rot13, unscramble first)
Steve555 wrote: Apologies, hopefully this will clarify my problem:
----- In Test.h: ----- class Test { public: Test(){}; virtual ~Test(){}; static long myVal; }; long Test::myVal;
The above line needs to be in a .cpp file, not in a header file. It
should only appear once in the entire code, not in every translation
unit that includes Test.h. That may be the source of your problem.
I'm surprised you didn't get a linker error when you compiled and
linked. ----- In main.cpp: ----- #include "Test.h" #include "GetVal.h" int main(int argc, const char *argv[]) { Test::myVal = 17; GetVal(); }
----- In GetVal.cpp ----- #include "Test.h" #include "GetVal.h" void GetVal() { long x = Test::myVal; }
----- In GetVal.h ----- void GetVal();
The problem is that when GetVal() is called, x =0
Uhhhmmm... How do you know? Both Alf and Jakob asks you to post the
tiniest possible source that shows the problem. How, exactly, does
this show the problem?
Also, please include some context when you reply to a message. Most
people don't use google groups, so they may not be able to figure out
that your most recent posting relates to the prior posts.
Best regards,
Tom Apologies, hopefully this will clarify my problem: ----- In Test.h: ----- class Test { public: Test(){}; virtual ~Test(){}; static long myVal; }; long Test::myVal;
The above line should go into one compilation unit only. Actually,
I
wonder why you did not get a linker error when linking main.cpp and
GetVal.cpp, because they both include Test.h and thus also both define
Test::myVal. ----- In main.cpp: ----- #include "Test.h" #include "GetVal.h" int main(int argc, const char *argv[]) { Test::myVal = 17; GetVal(); }
----- In GetVal.cpp ----- #include "Test.h" #include "GetVal.h"
You could insert it here, for example:
long Test::myVal;
void GetVal() { long x = Test::myVal; } ----- In GetVal.h ----- void GetVal();
The problem is that when GetVal() is called, x =0
Should be solved now.
Many thanks, Jakob, fixed it.
>How, exactly, does this show the problem?
How does posting a complete tiny program satisfy being asked to post a
complete tiny program??
No, sorry, you'll have to run that one by me again! Also, please include some context when you reply to a message.
Noted.
Thanks
Steve
Steve555 wrote: How, exactly, does this show the problem? How does posting a complete tiny program satisfy being asked to post a complete tiny program?? No, sorry, you'll have to run that one by me again!
Also, please include some context when you reply to a message.
Noted.
<SIGH>
Steve, you left out the context that answers your own question.
What I wrote was:
"Uhhhmmm... How do you know? Both Alf and Jakob asks you to post the
tiniest possible source that shows the problem. How, exactly, does
this show the problem? "
Your response was:
"How does posting a complete tiny program satisfy being asked to post a
complete tiny program??"
That's a non-sequitur. I wasn't taking issue that the program you
posted was sufficiently small - it was, and thank you for making it
small. I was pointing out that it didn't show the problem. There was
nothing showing that that the value of x was zero - we just had you
telling us that it was, but how did you know? Were you running the
program under a debugger? What I was trying to subtly point out - I
guess too subtly - is that the program you post needs to show the
error. Traditionally, one does that either by sending the apparently
wrong value to std::cout so we can see it, or by including an assert.
In other words, you post a program and say "I was expecting the output
to be [whatever], but when I compile and run it, instead the output is
[whatever]. Please help me understand why."
Anyway, I'm glad your problem was solved.
Best regards,
Tom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Scott Lyon |
last post by:
I'm maintaining (read: I didn't write it, nor do I have the time to spend to
rewrite it) an application that is suddenly giving me grief.
The reason I say suddenly, is because we're in the process of transitioning
the server on which it runs from Microsoft Windows 2000 Server, to 2003
server (going from IIS 5 to IIS 6). This problem hasn't really occurred on
the 2000 server machine, but it's happening MUCH more on the new 2003 box
(not...
|
by: yellow1912 |
last post by:
Hi, I'm very new to C#, in fact I dont know anything about it. Im
having problem with its static variables. I used static variables alot
in C++, but seem like C# doesnt allow it inside a function the way C#
do, so how can you handle recursive functions that need static
variable.
One more thing, I have a variable like this:
OracleDataReader dr = GetDataReader(sql,conn, out errMsg);
How can I make dr static to use in my recursive function?
|
by: Stephen |
last post by:
I have a .NET (1.1 framework) application that is losing a
session variable on only a few PC's. The main page is
loading up in a frame in a Portal application. On the
Page_Load it stores an object with the user id and
password into the session.
The web page includes a set of links. When the user
clicks on a link, another page within this application is
opened in a new window. On the Page_Load of this second
page, I am retrieving...
|
by: Simone Chiaretta |
last post by:
I've a very strange behaveour related to a website we built:
from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx code-behind and in business logic (C# classes used by the aspx) lose their value.
I cannot reproduce this on our development server, so I cannot understand what the cause of all this is.
We are using asp.net 1.1 with IIS6 on win2003.
|
by: cFleury |
last post by:
Hi,
I have a public structure which is initialized only at startup time but at
least one of the elements of this structure is losing its value, this
particular element is ONLY initialized at the same time the structure is also
initialized and nowhere else in the code. Worth mentioning here is that this
behavior (losing its value) ONY happens when the code is executed outside the
IDE, if executed from the IDE it will work ALLWAYS !!!
...
| |
by: Adrian Parker |
last post by:
We have a website that works everywhere but on a few PCs on this one site..
Asp.Net 1.1
Server = Windows 2003
Client = XP
In the web.config we use - cookieless="false"
in the browser settings they have "Always allow session cookies" set to true
When the browser connects to the website the first page sets a session
variable called "user_ref" to something and then calls another page. If on
|
by: jqheller |
last post by:
Folks I am losing my mind trying to work with the
TemplatedWizardStep. I have created a multistep user registration
form that collects plenty of user information including a password.
At the end of the process I am finding that the value of my password
control is blank. When I set the textmode of my textbox to
singleline, the value is preserved. It is just when the textbox is
set to password that I lose the value.
I am casting to the...
|
by: Philipp Kraus |
last post by:
Hi,
I have one problem with my position of my background image. I have
created one div-tag that has a background image, which should be fixed
during scrolling. On the left side of the div is my menu-div, so the
content div is shown round 25% from the left browser side. In this
content div I would like to position a image on the left or right side.
I need a background-attachment: static, because the layout position is
the div, not the...
|
by: teddysnips |
last post by:
Weird.
I have taken over responsibility for a legacy application, Access 2k3,
split FE/BE.
The client has reported a problem and I'm investigating. I didn't
write the application.
The AutoExec macro calls a function "InitApplication" which, in turn,
calls a function to set the value of a global string variable
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |