问题的标题是错误本身,但我也会将其包含在下面:
error: argument of type ‘char (CharStack::)()const throw (CharStack::Underflow)’ does not match ‘char’下面是我使用的代码文件:
#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;
}在头文件中,我定义了我使用的两个异常,如下面的示例所示:
public:
// exceptions
class Overflow{};
class Underflow{};我想这是因为我没有处理摘录,但我不知道在目前的情况下如何处理它。
谢谢
发布于 2012-03-09 04:27:22
return cur -> info;info是返回char的成员函数吗?那么你应该使用:
return cur -> info();否则,返回的是指向成员函数的指针,而不是char。
这一点相同:
char top = stack -> top;stack->top是成员函数指针,stack->top()是对stack对象的top函数的调用。
顺便说一句,如果empty()为真,你的top函数不会返回任何东西,这是非法的。你需要返回一个char或者抛出,但是离开函数不返回或者抛出是不正确的。
https://stackoverflow.com/questions/9624364
复制相似问题