Reputation: 858
I want to write my own version of stack, this is what I have:
template<class myStackType> class myStackClass
{
myStackType array[1000];
int size;
public:
myStackClass()
{
size = 0;
}
void pop()
{
size--;
}
void push(myStackType a)
{
array[size] = a;
size++;
}
myStackType top()
{
return array[size-1];
}
bool empty()
{
return(size == 0);
}
};
but when I try to actually use it
struct grade
{
int mid;
int final;
string name;
grade(int mid1 = 0, int final1 = 0, string name1 = 0)
{
mid = mid1;
final = final1;
name = name1;
}
};
myStackClass<grade> myStack;
I get a debug assertion failed: invalid null pointer
on the other hand, the std::stack works just fine in the same spot with the same data type
what am I doing wrong?
Thanks!
Upvotes: 0
Views: 329
Reputation: 39439
You are assigning 0 to a string in your constructor. That's the bug. The compiler is trying to interpret the 0 as a char *, i. e. a C-style string. But since it's a 0, it is interpreted as a NULL pointer.
You may also want to do some error checking to make sure your stack doesn't overflow, or that you don't try to pop off an empty stack.
Upvotes: 1
Reputation: 56921
This is wrong:
string name1 = 0
It tries to construct a string
from a const char*
which is 0
- and this is not allowed. You probably meant:
string name1 = ""
Upvotes: 2