473,543 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static variables in class

Sorry in advance if this message sounds imprecise but it's difficult to
be precise when you don't really understand what's going on.

I have a class called Parameters. The default constructor
Parameter:Param eter() contains various values such as interest = 0.05;

My intent is for the user to be able to override these defaults so I
have a function

void Parameter::set( void) . This is supposed to give the user a chance
to define variables differently.

I then include in main a statement of the form Parameter *params = new
Parameter;

To set the variables I have params->set();

When I tried to write these variables, I seemed to be getting default
values, not the values from the user.

So I hit on the probably bad idea of listing the default values as
static int, static double etc.

Not only did this make it not compile. It never compiled when I went
back and deleted the words "static". I got linker error messages and
they stayed with me even when I changed the program back to how it was.

Any help or advice would be appreciated.

If anyone would enjoy helping me via IM or email, that would also be
great.

Yahoo IM: pauldepstein
msn: pa**********@ho tmail.com
email: pa**********@ya hoo.com

Thank you very much.

Paul Epstein

Nov 30 '05 #1
3 1602
pa**********@at t.net wrote:
Sorry in advance if this message sounds imprecise but it's difficult
to be precise when you don't really understand what's going on.
Read FAQ 5.8.
I have a class called Parameters. The default constructor
Parameter:Param eter() contains various values such as interest = 0.05;
You probably mean that inside the body of the constructor you assign
values to your data members.
My intent is for the user to be able to override these defaults so I
have a function

void Parameter::set( void) . This is supposed to give the user a
chance to define variables differently.

I then include in main a statement of the form Parameter *params = new
Parameter;

To set the variables I have params->set();

When I tried to write these variables, I seemed to be getting default
values, not the values from the user.
Make sure that your 'set' function does not _redeclare_ the data members
you're trying to set. If there is a declaration of, say, 'interest' in
the 'set' function, remove it.
So I hit on the probably bad idea of listing the default values as
Why do you call them "default values"?
static int, static double etc.
Probably. If those values are declared 'static', remove 'static'.
Not only did this make it not compile.
Did what made what not compile?
It never compiled when I went
back and deleted the words "static".
Went back where?
I got linker error messages and
they stayed with me even when I changed the program back to how it
was.
How was it?
Any help or advice would be appreciated.


Read the FAQ. Here: http://www.parashift.com/c++-faq-lite/

V
Nov 30 '05 #2

Victor Bazarov wrote:
Make sure that your 'set' function does not _redeclare_ the data members
you're trying to set. If there is a declaration of, say, 'interest' in
the 'set' function, remove it.
Hopefully, this is exactly the problem.

However, here is some code that my teacher (an accomplished programmer)
produced.

Parameter::Para meter ()
{
// Default constructor. Here defaults can be given to any private
// variables. Two examples are given. These can be changed by the
// set() member function.

solve_type = 1;
compute_error = 0;
}

//=============== =============== =============== =============== ===============//

void Parameter::set (void)
{
// Member function for setting the parameters.

cout << "Enter nj, " << "length, " << "final time, "
<< "number of time steps, " << "theta" << endl;

cin >> nj >> length >> t_f >> nsteps >> theta;

dt = t_f / nsteps;
dx = length / nj;

compute_error = 1;
}

So you see that compute_error is being reset, which seems to be exactly
what you told me not to do. My situation was different because I had
statements like

cout << "interest";
cin >> interest;

in my Parameter::set function, why should resetting interest in this
way fail when, in my teacher's example, compute_error is being
successfully reset?

On second thoughts, "redeclare" probably means using types such as
double interest = 0.05;

No, I do not have different declarations of the form double interest =
.....;

However, after declaring interest in a header file, parameter.h,

I have a statement interest = ...; in parameter::para meter

and another statement cin >> interest; in parameter::set.

This seems to me exactly what my teacher did.

So I hit on the probably bad idea of listing the default values as


Why do you call them "default values"?


I call them default values because I set them in the default
constructor Parameter::Para meter()
static int, static double etc.
Probably. If those values are declared 'static', remove 'static'.


That's exactly what I did.
Not only did this make it not compile.
Did what made what not compile?


When I gave the variables in the default constructor static types, my
project did not compile.
It never compiled when I went
back and deleted the words "static".
Went back where?


This is what seemed to be happening. I started with a version that
compiled successfully. I I then wrongly made the variables in my
default constructor static types. I got linker error messages when I
tried to compile. So I did the obvious thing, and deleted the words
"static". However, the program gave the same linker error messages.
In other words, the word "static" appeared to cause compiler errors
that were permanent in the sense that the errors stayed even when I
tried to correct and recompile by deleting the word "static".
I got linker error messages and
they stayed with me even when I changed the program back to how it
was.
How was it?


It was a version that compiled and ran successfully but gave me the
wrong values. I am now stuck in a situation where my version doesn't
compile.
Any help or advice would be appreciated.
Read the FAQ. Here: http://www.parashift.com/c++-faq-lite/

I have done this. Part of the problem is that I'm not sure exactly
which part of my code to iinclude. It isn't so short. I already have
a new line of inquiry that the problem may be that the parameter::set
function contradicts the default constructor Parameter::Para meter
(thanks for this insight.)

So now, I would like to understand why this apparent contradiction is
o.k. in my teacher's code which is pasted above.

Thank you for your help.

Paul Epstein
V


Nov 30 '05 #3
pa**********@at t.net wrote:
Victor Bazarov wrote:
Make sure that your 'set' function does not _redeclare_ the data
members you're trying to set. If there is a declaration of, say,
'interest' in the 'set' function, remove it.
Hopefully, this is exactly the problem.

However, here is some code that my teacher (an accomplished
programmer) produced.

Parameter::Para meter ()
{
// Default constructor. Here defaults can be given to any private
// variables. Two examples are given. These can be changed by the
// set() member function.

solve_type = 1;
compute_error = 0;


OK, so those things are data members, most likely. How are they declared
in the 'Parameter' class? IOW, why haven't you posted what your "Parameter"
class looks like?

}

//=============== =============== =============== =============== ===============//

void Parameter::set (void)
{
// Member function for setting the parameters.

cout << "Enter nj, " << "length, " << "final time, "
<< "number of time steps, " << "theta" << endl;

cin >> nj >> length >> t_f >> nsteps >> theta;
So, 'nj', 'length', 't_f', 'nsteps', 'theta', are probably all data members
as well. But I am only guessing here, no way to be sure without the class
definition.

dt = t_f / nsteps;
dx = length / nj;
And these 'dt' and 'dx', too. OK.

compute_error = 1;
And 'compute_error' ...
}

So you see that compute_error is being reset, which seems to be
exactly what you told me not to do.
What's the importance of that? How is that relevant to what you initially
asked about?
My situation was different
because I had statements like

cout << "interest";
cin >> interest;

in my Parameter::set function, why should resetting interest in this
way fail when, in my teacher's example, compute_error is being
successfully reset?
Could it be that 'interest' is not a data member? Impossible to tell
without seeing the class definition, again.
On second thoughts, "redeclare" probably means using types such as
double interest = 0.05;
Yes, it would.
No, I do not have different declarations of the form double interest =
....;
OK.
However, after declaring interest in a header file, parameter.h,

I have a statement interest = ...; in parameter::para meter

and another statement cin >> interest; in parameter::set.

This seems to me exactly what my teacher did.
Fine. I can't advise because you aren't showing enough code.
[..]
It never compiled when I went
back and deleted the words "static".
Went back where?


This is what seemed to be happening. I started with a version that
compiled successfully. I I then wrongly made the variables in my
default constructor static types. I got linker error messages when I
tried to compile.


That sounds about right. Static data members have to be defined in
addition to being declared. You probably forgot to define them outside
the class definition. Read up on statics in order to avoid future
mistakes.
So I did the obvious thing, and deleted the words
"static". However, the program gave the same linker error messages.
Probably because you didn't save your code or didn't _recompile_.
In other words, the word "static" appeared to cause compiler errors
that were permanent in the sense that the errors stayed even when I
tried to correct and recompile by deleting the word "static".


You didn't recompile, most likely.
I got linker error messages and
they stayed with me even when I changed the program back to how it
was.


How was it?


It was a version that compiled and ran successfully but gave me the
wrong values. I am now stuck in a situation where my version doesn't
compile.


Well, until I see more code, I can't recommend any solution. It would
mean guessing on my part and I don't like guessing. Besides, you seem
to have done enough guessing in your project already.

The usual approach to fixing the program is "divide and conquer".
Start by removing (commenting out) all data members, and any mention
of them in the class implementation. Make sure the empty framework
then compiles. Then start adding one by one.

Functionality should not be added in bulk. It's difficult to manage
any mistakes that way. Make small steps. Make sure the program works
every time you decide to compile it after making a complete change.
And again, remember to move in small increments.

V
Nov 30 '05 #4

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

Similar topics

1
3661
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab, turned in, graded and passed so I'm not trying to get someone to do my lab assignments, but after I got this back I was reading about the...
9
6349
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you...
9
2295
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts that the app uses. So I will need to change the fonts depending on what settings the user has entered. However, it seems kind of wasteful to me to go...
4
10739
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and inherited classes are all identical. I need to refer to the static variables in each class, and each must maintain its own values for each static...
28
4580
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. Would the performance be better using Static versus Dynamic. I would think it would be quicker with STATIC declarations since the variables would...
5
6761
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that (1) holds for unmanaged C++ code, but not for managed code. I have class library with both managed and unmanaged static variables that are not...
5
3588
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles fine. 1 / Now If I move the getSpectrumAtDistance( const T &dist ) method definition in SpectralProfofile.cc let's say the compiler says ...
55
6155
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
10
2633
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
16
8592
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions - which is Very Bad (tm) for reasons I understand! However, does this rule apply only to global static variables, or does it apply to...
0
7408
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
7349
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
7590
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. ...
1
7347
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5885
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...
1
5271
isladogs
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...
0
4895
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...
1
1817
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
0
636
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.