stack issue

i am trying to implement a stack that keeps throwing an exception and says unable to read memory.
its working fine with int and even char when i tested them alone using push and pop functions but not in this loop case i don't know why

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template<class type>
void stackt<Dtype>::push(type s)
{
	if (stackisfull())
		cout << "stack overflow" << endl;
	else
	{
		
		stack[++top] = s;
	}
}
// in main 
       stackt<char> c;
       string x;
       cin>>x;
	c.push('N');
	int a = x.size();
	for (int i = 0; i < a; i++)
	{

		char z = x[i];
		c.push(z);
	}
In what way is it not working the way you want?

You use the word "exception" but your code doesn't use exceptions. The push function prints an error message but that is not the same thing as throwing an exception. Is that the problem? You want an exception to be thrown so that the loop is aborted?
i am getting an 'unable to read memory' error, and
'Exception thrown: write access violation.
this->**stack** was 0x1110112. occurred'

the function isn't working at all now, whenever i push anything i get the same error
What is the size of stack?
Is stackisfull() implemented correctly?
Does the stackt constructor initialize its member variables correctly?
i copied the same exact functional stack from my other project that's 100% working, yet it keeps hitting me with these exceptions in this project i don't know why
Could you show the code for the constructor and for stackisfull()?
i figured it out, i wasn't even building the stack in the default constructor
Thanks anyway
Topic archived. No new replies allowed.