错误:在类方法声明中无效地使用void表达式

error: Invalid use of void expression , in a class method declaration

本文关键字:void 表达式 无效 类方法 声明 错误      更新时间:2023-10-16

事情是这样的。我试图建立一个EditorBuffer类(单行文本编辑)。

类由两个字符堆栈(stack<char> before , stack<char> after)组成,其中前堆栈表示位于"光标"之前的所有字符,后堆栈表示位于"光标"之后的所有字符。在下面的声明中,我得到了错误error: Invalid use of void expression,这对我来说是完全陌生的。

方法声明:

void EditorBuffer::moveCursorToEnd()
{
    while (!after.empty())
    {
        before.push(after.pop());
    }
}

std::stack::pop()不返回任何东西(它的返回类型是void)。你可能想这样做:

before.push(after.top());
after.pop();