Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 23rd, 2009, 12:04 AM   #1
CFlower
Newbie
 
Join Date: Nov 2009
Posts: 6
Rep Power: 0 CFlower is on a distinguished road
Problem with pointer in function return

hi all ,
i have problem and three days just trying find out, but no result!!

i have two diffirent classes: Xnode and Ynode
in Ynode, i have method called tree() which it gets as argument - a reference of Xnode (as const) , and it calls its method to return one private variable xx.

void Ynode::tree(const Xnode& x){
       const Node* n=x.list();
       std::cout << n->value;
}
const  Node* Xnode::list() const {
         const Node* nod=xx;
         std:cout << nod->value;
         return nod;
}
the output inside Xnode was good value(as expected) , but the second (after return from list()) -was undefined value - ...56923434.
can you tell me what's wrong? why the pointer is undefined in
const Node* n=x.list(); ??

its little urgent!.

thanks a lot.

Last edited by Ancient Dragon; Nov 23rd, 2009 at 5:31 AM. Reason: replaced icode tags with code tags
CFlower is offline   Reply With Quote
Old Nov 23rd, 2009, 2:18 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 7,021
Rep Power: 12 DaWei will become famous soon enough
Re: Problem with pointer in function return

Yep. You haven't learned about scope. You might want to Google it (disregard any links regarding mouthwash).

Incidentally, it isn't urgent. You might want to also Google Eric Raymond's "Smart Questions."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.

Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
Politically Incorrect
DaWei is offline   Reply With Quote
Old Nov 23rd, 2009, 8:32 AM   #3
CFlower
Newbie
 
Join Date: Nov 2009
Posts: 6
Rep Power: 0 CFlower is on a distinguished road
Re: Problem with pointer in function return

hi,
Do you think i didnt try googling or learning before posting? i did.
but this kind of Q is not easy to solve and needs epecially assistance from others who can advise.

thanks anyway
CFlower is offline   Reply With Quote
Old Nov 23rd, 2009, 9:20 AM   #4
Ancient Dragon
PFO God In Training

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 1,534
Rep Power: 6 Ancient Dragon will become famous soon enough
Re: Problem with pointer in function return

in list(), what is xx?

>>was undefined value - ...56923434.
That's not undefined value, but uninitialized value. undefined means you tried to use a variable that had not been declared. uninitialized means you used a variable that had been declared but not set to anything; it just contains whatever random data was in memory at the time the variable was declared.
__________________
PFO's FAQ is here
Ancient Dragon is offline   Reply With Quote
Old Nov 23rd, 2009, 12:19 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 7,021
Rep Power: 12 DaWei will become famous soon enough
Re: Problem with pointer in function return

I gave you the correct term to Google. Here are the first two paragraphs from the first hit for C++ scope:

Quote:
C++ names can be used only in certain regions of a program. This area is called the "scope" of the name. Scope determines the "lifetime" of a name that does not denote an object of static extent. Scope also determines the visibility of a name, when class constructors and destructors are called, and when variables local to the scope are initialized. (For more information, see Constructors and Destructors.) There are five kinds of scope:

*

Local scope A name declared within a block is accessible only within that block and blocks enclosed by it, and only after the point of declaration. The names of formal arguments to a function in the scope of the outermost block of the function have local scope, as if they had been declared inside the block enclosing the function body. Consider the following code fragment:
This tells you that nod is only valid inside the function where it is defined.

Search is the first part. The second is the ability to read and comprehend. The are requirements for a good programmer because the time economy is important.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.

Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
Politically Incorrect
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
global vs local pointer with linked lists aatyler C 7 Sep 10th, 2009 8:02 AM
having problem need some help gentleboy C++ 8 Apr 4th, 2009 10:04 PM
simple pointer array w/ function crash codylee270 C 8 Dec 4th, 2005 9:28 PM
Return Strings in Function hbweb500 C# 8 Jul 10th, 2005 9:44 PM
Pointers in C (Part II) Stack Overflow C 2 Apr 29th, 2005 11:39 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:40 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC