gpt4 book ai didi

c++ - 重载函数错误消息

转载 作者:行者123 更新时间:2023-11-28 00:34:58 25 4
gpt4 key购买 nike

该程序应该通过仅使用一个变量和任意数量的堆栈来检查字符串是否为回文。它给了我一些与重载函数相关的错误消息,但我看不到我在哪里重载了它。还是我声明堆栈错误?我在他们引用的代码行之后在错误消息中进行了评论。谢谢!

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;
}

}

最佳答案

std::stack模板类只有成员函数 top()pop()定义。相反,您正试图将它们作为公共(public)成员对象访问。即使没有传递参数,函数调用也需要使用括号。

只需将所有出现的 .top.pop 替换为 .top().pop() 分别和你的程序 should at least compile .

另外,请习惯使用 std:: 前缀,而不是使用 using namespace std 包含整个 std 命名空间。从长远来看,这将为您避免潜在的麻烦。

关于c++ - 重载函数错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21292333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com