没有变量声明为函数,但错误:二进制表达式的操作数无效

No variable declared as function, but Error: invalid operands to binary expression

本文关键字:二进制 表达式 无效 操作数 错误 变量 声明 函数      更新时间:2023-10-16

我正在尝试编写一个程序来删除格式不正确的C++程序中的前导空格。我在第 24 行收到此错误:cout << removeLeadingSpaces(s) << endl;,请帮助。

#include <iostream>
using namespace std;
string removeLeadingSpaces(string line)
{
bool start = false;
string newline;
for (int i = 0; i < line.size(); i++)
{
    if (!isspace(line[i]) && start==false)
    {
    start = true;       
    }
    if (start==true)
        newline += line[i];
 }
return newline;
 }
 void printindented()
{
string s;
while (getline(cin, s))
    cout << removeLeadingSpaces(s) << endl;
}
int main() 
{
    cout << printindented() << endl;
}

有问题的行是

cout << printindented() << endl;

main.该行

cout << removeLeadingSpaces(s) << endl;

printindented就好了。

main中的行更改为:

printindented();

在 int main 中,您尝试打印一个返回导致错误的函数void。此外,您应该使用适当的缩进,下次通过更深入地了解问题来更好地表达您的问题。固定代码

int main() 
{
    printindented(); // Can't print void cout << printindented() << endl;
}