LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-10-2003, 02:17 AM   #1
chens_83
Member
 
Registered: Dec 2001
Location: Adelaide , South Australia
Distribution: redhat 7.2, Debian , OpenBSD
Posts: 123

Rep: Reputation: 15
returning a reference to Object C++


i have a template stack class that holds structure objects .. The trouble comes when i want to play around with the top element of the stack
I use this code
Code:
T& top(){
return stck[tos];
}
and in main or another function

Code:
stack <entry*> ob;
entry *temp = new entry;
temp->s = 12;
temp->t = "adfadsf";
ob.push(temp);
entry = ob.top(); //this doesnt work : /
this works for the pop() function but not for top !!!
 
Old 10-10-2003, 06:37 AM   #2
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
Please send us the implmentation of your push and pop functions. You may firs --'d the tos and then returned it for the pop. And for the push first store the value and then ++'d the tos : stack[tos]++ = PushVal;
 
Old 10-10-2003, 10:37 AM   #3
chens_83
Member
 
Registered: Dec 2001
Location: Adelaide , South Australia
Distribution: redhat 7.2, Debian , OpenBSD
Posts: 123

Original Poster
Rep: Reputation: 15
Code:
		template <class T>const T& stack<T>::top(){
			return stck[size];	
		}

		template <class T> void stack<T>::push(const T& x){
			
			if(size == 100 ){
				cout << "Stack is full" << endl;
			}

			else{
				stck[size] = x;
				size ++;
			}
		}

		template <class T> T stack<T>::pop(){

			if(size == 0 ){
				return 0;
			}

			else{
    		   tos --;
				return stck[size];
			}
		}
 
Old 10-10-2003, 09:16 PM   #4
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
It looks to me like you would want

Code:
template <class T>const T& stack<T>::top()
{
  return stck[size-1];
}
Since size is always pointing just above the stack top .

Cheers..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Reference problem in Java servlets Hockeyfan Programming 1 06-16-2005 01:36 AM
compile error while returning STL iterator as object * rameshmiraje Programming 2 06-20-2004 01:50 PM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM
Returning an object Mohsen Programming 12 08-07-2003 02:41 AM
having trouble returning... jhorvath Programming 12 10-26-2002 10:22 AM


All times are GMT -5. The time now is 05:15 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration