C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

class definition forgets values

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
rene
Guest





PostPosted: Thu Jan 27, 2005 3:20 am    Post subject: class definition forgets values Reply with quote



Hi all,
please look into this small piece of code and tell me why my values
weren't stored. Ive searched for hours on an explanation, but found
none.
Ive got an class with (in this example) 2 ints and want to set an read
them
out of another implementation. BUT i always get zeros back.
Ive also tried that with pointer (eg mycur=new curve()Wink but same
result.

Thanks in advance for any reply
Rene

class definition:

class curve
{
public:
curve()
{
}
int ckeytime;
int ckeyspeed;

void setCurvekeyTime (long t)
{
ckeytime=t;
}

void setCurvekeySpeed (long s)
{
ckeyspeed=s;
}

int CurvekeyTime() {
return ckeytime;
}
int CurvekeySpeed() {
return ckeyspeed;
}
};

implementation other class:
public: curve mycur;

one void:
mycur.setCurvekeyTime(11);
mycur.setCurvekeySpeed(12);

other void:
cout << mycur.CurvekeyTime() << endl;
cout << mycur.CurvekeySpeed() << endl;

results in:
0
0

Back to top
Ian McCulloch
Guest





PostPosted: Thu Jan 27, 2005 3:30 am    Post subject: Re: class definition forgets values Reply with quote



rene wrote:

Quote:
Hi all,
please look into this small piece of code and tell me why my values
weren't stored. Ive searched for hours on an explanation, but found
none.
Ive got an class with (in this example) 2 ints and want to set an read
them
out of another implementation. BUT i always get zeros back.
Ive also tried that with pointer (eg mycur=new curve()Wink but same
result.

Can you post a minimal, COMPLETE & COMPILABLE section of code that
demonstrates the problem?

Ian


Back to top
rene
Guest





PostPosted: Thu Jan 27, 2005 3:33 am    Post subject: Re: class definition forgets values Reply with quote



Hi Ian,
no I couldn't.
The whole code is 16000 lines long.
If I write a little example code. Everythis works fine.
Yes, could be a bug in the compiler, but Ivr tried several gccs and
iccs.
Same result

Back to top
shez
Guest





PostPosted: Thu Jan 27, 2005 3:45 am    Post subject: Re: class definition forgets values Reply with quote


rene wrote:
Quote:
implementation other class:
public: curve mycur;

one void:
mycur.setCurvekeyTime(11);
mycur.setCurvekeySpeed(12);

other void:
cout << mycur.CurvekeyTime() << endl;
cout << mycur.CurvekeySpeed() << endl;

results in:
0
0

Are you sure you're calling the first "void"? Print something there to
make sure your program actually calls the set functions.

BTW, we have a name for these "voids"... they're called "functions" (or
"methods") ;)

-shez-


Back to top
rene
Guest





PostPosted: Thu Jan 27, 2005 3:59 am    Post subject: Re: class definition forgets values Reply with quote

Are you sure you're calling the first "void"?
Yes, if sourrounded the code with debugging stuff.

the FUNCTION (<- you know)
mycur.setCurvekeyTime(11);
is OK.
debugging cout in the curve class tells me, that 11 is assigned to t
and ckeytime.
But calling mycur.CurvekeyTime() holds the value 0.
So something went wrong with the return value.
If I changed the code to:
int CurvekeyTime() {
//return ckeytime;
return 123;
}
I get the 123.

BTW. Please forgive the typo with long and int. There are all long in
my code.
I have acted desperately and set them all to int.

Tnx Rene

Back to top
Ian McCulloch
Guest





PostPosted: Thu Jan 27, 2005 5:12 am    Post subject: Re: class definition forgets values Reply with quote

rene wrote:

Quote:
Hi Ian,
no I couldn't.
The whole code is 16000 lines long.
If I write a little example code. Everythis works fine.
Yes, could be a bug in the compiler, but Ivr tried several gccs and
iccs.
Same result

Almost no chance it is a compiler bug.

The only thing I can suggest is that you systematically either expand your
working example to the point that it demonstrates the problem, or you strip
down the full code down towards the example, until it becomes clear where
the bug is.

HTH,
Ian


Back to top
teejbee@hotmail.com
Guest





PostPosted: Thu Jan 27, 2005 8:50 am    Post subject: Re: class definition forgets values Reply with quote

Usually these sorts of problems occur when the code where you set the
value and the code where you read the value actually do not talk to the
same instance of the class. Make *sure* you are dealing with the same
instance of curve class and as posted earlier, you are calling the
setCurvekeyTime() before CurvekeyTime() call.

If you have access for modifying the code, try to print (or by any
other means) check the value of "this" pointer in both the
setCurvekeyTime() and CurvekeyTime() functions.

Hope this helps,
TJ

Back to top
rene
Guest





PostPosted: Thu Jan 27, 2005 12:20 pm    Post subject: Re: class definition forgets values Reply with quote

Hi TJ,

tried this. Won't help.
Got a question. Do I need that constructor to initialize the integers
(ckeytime, ckeyspeed)?
Will the mem space reserved by initialize the class curve(normal or as
pointer; curve mycur or. mycur= new curve.)

If also tried the set these values via the constructor.
curve::curve(int a, int b){ckeytime=a;ckeyspeed=b;};
mycur=new curve(12,13);

But It don't work either.

Back to top
TJ
Guest





PostPosted: Fri Jan 28, 2005 12:27 pm    Post subject: Re: class definition forgets values Reply with quote

Quote:
tried this. Won't help.
So you have confirmed that you get same value of 'this' pointer and

hence effectively talking to the same instance in both the places?

Quote:
Got a question. Do I need that constructor to initialize the integers
(ckeytime, ckeyspeed)?
Hmm... Not necessary, but it is a very good practise to initialize all

the member variables in the constructor.

When you create an instance as a local (aka automatic) variable, the
memory space will not be initialized; as is the case of curve mycur.
Whereas when you create an instance in the heap (new curve), the memory
space is initialized to zero by *most* of the compilers.

Setting the values via constructor or via any set functions, does not
matter much. Look out for other places in the code where you change
the value of the variables.

By the way what is the value you get when you query for the variables?
Do you get zero (and you have intialized to 0 in the ctor) or you get
junk?


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.