1 Replies - 560 Views - Last Post: 14 December 2010 - 09:59 AM Rate Topic: -----

#1 Ario7X   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 24-April 10

no nodes in stack

Posted 14 December 2010 - 05:37 AM

#include <iostream>
#include <list>

using namespace std;

class a{

public:

        char *myname;

	a(){
        myname = " HH ";
	}

};

class b{

public:
	list<a *> stack1;
	list<a *> stack2;

	void push(a *ob, list<a *> stacks){

		list<a *>::iterator it;

		for ( it=stack2.begin() ; it != stack2.end(); it++ ){

			if( (*it) == ob  ) return;

		}

		stacks.push_front(ob);

}

		void printstack(list<a *> stacks){

		cout << " Nodes in stack are " << endl;

		list<a *>::iterator it;

		for ( it=stacks.begin() ; it != stacks.end(); it++ ){
                cout << (*it)->myname << endl;
		}
}

void insertobstoStack(){

a *ob= new a();

	a *ob2 = new a();
	a *ob3 = new a();

	push(ob, stack1);
	push(ob2 , stack1);
	push(ob3 , stack1);
    
printstack(stack1);

}

};


int main(){

b *object = new b();

object->insertobstoStack();

	return 0;
}



When i run this program it does not print any nodes in the stack

Where have i gone wrong ?

Is This A Good Question/Topic? 0
  • +

Replies To: no nodes in stack

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2408
  • View blog
  • Posts: 4,542
  • Joined: 30-May 10

Re: no nodes in stack

Posted 14 December 2010 - 09:59 AM

The stacks parameter in push() is a copy (a short-lived copy).

I think you need to make it a reference if you want to push into something called stack1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1