static variables in class

P

pauldepstein

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: (e-mail address removed)
email: (e-mail address removed)

Thank you very much.

Paul Epstein
 
V

Victor Bazarov

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
 
P

pauldepstein

Victor said:
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.

Why do you call them "default values"?

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

That's exactly what I did.
Did what made what not compile?

When I gave the variables in the default constructor static types, my
project did not compile.
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".
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.
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

Victor Bazarov

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.
[..]
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top