2014-01-22 79 views
0

该程序应该通过只使用一个变量和任意数量的栈来检查字符串是否是回文。它给我提供了一些与重载函数有关的错误消息,但是我看不到它超载的地方。还是我宣布堆栈错了?我在他们提到的代码行之后的错误消息中发表了评论。谢谢!重载函数错误消息

int main() 
{ 
    stack <char> stackA; 
    stack <char> stackB; 
    stack <char> stackC; 

    char avariable; 
    cout << "Enter a string: "; 
    avariable = cin.get(); 
    while(avariable != '\n') 
    { 
     if(avariable != ' ') 
     { 

      stackA.push(avariable); 
      stackB.push(avariable); 
      avariable = cin.get(); 
     }} 

    stackC.push('$'); 

    while(!stackB.empty()) 
    { 
     avariable = stackB.top; //error, cannot resolve overloaded function 'top' based on conversion to type 'char' 
     stackC.push(avariable); 
     stackB.pop; //statement cannot resolve address of overloaded function 
    } 

    avariable = '$'; 
    stackC.push('$'); 

    while(!stackA.empty()) 
    { 
     if(avariable == stackC.top) //invalid operands of type 'char' and <unresolved overloaded function type>' to binary 'operator==' 
     { 
      avariable = stackA.top; //cannot resolve overloaded function 'top' based on conversion to type 'char' 
      stackA.pop; //cannot resolve address of overloaded function 
      stackC.pop; //cannot resolve address of overloaded function 
     } 
     else 
     { 
      cout << "The string of characters entered is not a palindrome." << endl; 
     } 
    } 

    if (stackC.top == '$') //invalid operands of type 'char' and <unresolved overloaded function type>' to binary 'operator==' 
    { 
     cout <<"The string of characters entered is a palindrome." << endl; 
    } 

} 

回答

2

std::stack模板类仅有构件功能top()和定义pop()。您正尝试将它们作为公共成员对象访问,而不是。即使没有参数传递,函数调用也需要使用括号。

只需更换的.top.pop所有出现到.top().pop()分别与程序should at least compile

也请适应std::前缀,而不是包括整个std命名空间与using namespace std。从长远来看,它将为您节省潜在的麻烦。

+0

为什么你说为每个实例使用std ::更好?我被一位教授告诉了相反的情况,尽管我不记得他解释了它的好处。 – Neko

+0

@Neko,[Read here](http://stackoverflow.com/q/1452721/493122)。 – Shoe

+0

谢谢!我想我应该在查问之前查看它。 – Neko

1

要调用一个函数,你需要括号:

stackB.top() 
stackB.pop() 

由于两个toppop没有参数,括号是空的,但仍然需要。

例如,这与您所做的stackA.empty()cin.get()没有什么不同。