generic stack

I'm trying to create a generic stack with the help of union. I get some kind of error with "some" not defined!!

#include<iostream>

using namespace std;


union un
{
int no;
char ch;
double dub;
};

class stack
{
int size;
int top;
public:

stack()
{

size=50;
top=-1;
}
void push1(int);
void push2 (double);
void push3(char);
bool isFull();
bool isEmpty();
void push(un);
char pop();

};


int main()
{
char q='Y';
int tag,tag_no[50];
stack generic;
un* some;
some= new un [50];

do
{
cout<<"which element you want to input? \n 1.integer \n 2.double \n 3.character";
cin>>tag;
switch(tag)
{
case 1:
{
cout<<"You chose integer \n";
cin>>some->no;
generic.push1(some->no);
break;
}
case 2:
{cout<<"You chose double \n";
cin>>some->dub;
generic.push2(some->dub);
break;}

case 3:
{cout<<"You chose char \n";
cin>>some->ch;
generic.push2(some->ch);
break;}

default:
{cout<<"Wrong! "; break;}
}
cout<<"Enter again?? :"<<endl;
cin>>q;
}
while (q=='Y' || 'y');
return 0;


}

void stack::push1(int i)
{
if (!isFull())
some[++top]->i;
}

void stack::push2(double d)
{
if (!isFull())
some[++top] =d;
}

void stack::push3(char c)
{
if (!isFull())
some[++top] =c;
}

bool stack::isFull()
{
if (top<size) return 0;
else return 1;
}

bool stack::isEmpty()
{
if (top==-1) return 1;
else return 0;
}

class stack knows nothing about local variable some defined in main. So for example the compiler will complain about name some used in member function
push1

void stack::push1(int i)
{
if (!isFull())
some[++top]->i;
}
where would i put it then? shows an error when put in class.
I do not know why you have to use this variable.
If you want a generic stack, use templates. Unions are silly and not at all what you want for this.
I think you meant something as

class stack
{
int size;
int top;
un *some;
public:
// other stuff
Last edited on
Topic archived. No new replies allowed.