The title of the question is the error itself, but I will also include it below:
error: argument of type ‘char (CharStack::)()const throw (CharStack::Underflow)’ does not match ‘char’
Here is the code file that I am using:
#include <iostream>
#include "CharStack.h"
using namespace std;
// returns top value on stack
// throws exception if empty
//
// O(n)
char CharStack::top() const throw( Underflow )
{
Elem * cur = head;
if( !empty() )
{
while( cur && cur -> next )
cur = cur -> next;
return cur -> info;
}
}
int main()
{
CharStack * stack = new CharStack();
char top = stack -> top;
stack -> push( 't' );
stack -> push( 'e' );
stack -> push( 's' );
stack -> push( 't' );
stack -> push( 'i' );
stack -> push( 'n' );
stack -> push( 'g' );
stack -> output( cout );
delete stack;
}
In the header file I define the two exceptions I use, as an example I am following does:
public:
// exceptions
class Overflow{};
class Underflow{};
I assume it’s because I am not handling the excerption, but I do not how to handle it in this current situation.
Thank you
Is
infoa member function that returns achar? Then you should be using:otherwise you’re returning a pointer-to-member-function, not a char.
Same for this:
stack->topis a member function pointer,stack->top()is a call to thetopfunction of thestackobject.BTW, your
topfunction doesn’t return anything ifempty()is true, which is illegal. You need to either return achar, or throw, but leaving the function without returning or throwing is ill-formed.