错误:请求从“ void”转换为“ QString”的非量表类型


#include "mainwindow.h"
#include <QApplication>
#include <QString>
#include <QDebug>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    QString h;
    h = "fsdfsdfsf";
    QString j = h.chop(3);
    qDebug() << "j: " << j;
    return a.exec();
}

输出:

error: conversion from ‘void’ to non-scalar type ‘QString’ requested

我在哪里出错?

错误的原因是 QString::chop()删除了该字符串的字符,并且不会返回任何内容(返回void)。

您有两个选择:

  • 设置j = h,然后致电j.chop (3);

  • 使用QString::chopped()j = h.chopped (3)

最新更新