473,506 Members | 16,201 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:Parameter() 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**********@hotmail.com
email: pa**********@yahoo.com

Thank you very much.

Paul Epstein

Nov 30 '05 #1
3 1599
pa**********@att.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:Parameter() 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::Parameter ()
{
// 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::parameter

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::Parameter()
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::Parameter
(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**********@att.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::Parameter ()
{
// 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::parameter

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
3660
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,...
9
6347
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...
9
2292
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...
4
10735
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...
28
4574
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. ...
5
6759
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...
5
3585
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...
55
6151
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...
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 -...
0
7105
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
7308
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
7371
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...
1
7023
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
4702
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...
0
3188
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...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
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...

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.