Qt中使用+=运算符时出错



我在Qt Creator中创建了一个小应用。我想在我的QDialog构造函数中使用这段代码,但是它不起作用。

std::string wyniki = "apg -q -n " + n + " -m " + m + " -x " + sx + " -a " + a;
if(exclude != "") wyniki+=" -E " + exclude.toUtf8().constData();
if(a==1)wyniki += " -M " + mode;
std::string result = exec(wyniki.c_str());
ui->plainTextEdit->setPlainText(qstr(result));

编译器消息:

../APG-GUI/scores.cpp: In constructor 'Scores::Scores(QWidget*, int, int, int, int, QString, QString)':
../APG-GUI/scores.cpp:36:45: error: invalid operands of types 'const char*' and 'const char [5]' to binary 'operator+'
     std::string wyniki = "apg -q -n " + n + " -m " + m + " -x " + sx + " -a " + a;
                                             ^
../APG-GUI/scores.cpp:37:67: error: invalid operands of types 'const char [5]' and 'const char*' to binary 'operator+'
     if(exclude != "") wyniki+=" -E " + exclude.toUtf8().constData();
                                                                   ^
../APG-GUI/scores.cpp:38:20: error: no match for 'operator+=' (operand types are 'std::string {aka std::basic_string<char>}' and 'const QString')
     if(a==1)wyniki += " -M " + mode;
                    ^
../APG-GUI/scores.cpp:38:20: note: candidates are:
In file included from /usr/include/c++/4.9/string:52:0,
                 from /opt/Qt/5.3/gcc_64/include/QtCore/qstring.h:50,
                 from /opt/Qt/5.3/gcc_64/include/QtCore/qobject.h:49,
                 from /opt/Qt/5.3/gcc_64/include/QtWidgets/qwidget.h:46,
                 from /opt/Qt/5.3/gcc_64/include/QtWidgets/qdialog.h:45,
                 from /opt/Qt/5.3/gcc_64/include/QtWidgets/QDialog:1,
                 from ../APG-GUI/scores.h:4,
                 from ../APG-GUI/scores.cpp:1:
/usr/include/c++/4.9/bits/basic_string.h:949:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator+=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
       operator+=(const basic_string& __str)
       ^
/usr/include/c++/4.9/bits/basic_string.h:949:7: note:   no known conversion for argument 1 from 'const QString' to 'const std::basic_string<char>&'
/usr/include/c++/4.9/bits/basic_string.h:958:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator+=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
       operator+=(const _CharT* __s)
       ^
/usr/include/c++/4.9/bits/basic_string.h:958:7: note:   no known conversion for argument 1 from 'const QString' to 'const char*'
/usr/include/c++/4.9/bits/basic_string.h:967:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator+=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
       operator+=(_CharT __c)
       ^
/usr/include/c++/4.9/bits/basic_string.h:967:7: note:   no known conversion for argument 1 from 'const QString' to 'char'
../APG-GUI/scores.cpp:39:45: error: no matching function for call to 'Scores::exec(const char*)'
     std::string result = exec(wyniki.c_str());
                                             ^
../APG-GUI/scores.cpp:39:45: note: candidate is:
In file included from /opt/Qt/5.3/gcc_64/include/QtWidgets/QDialog:1:0,
                 from ../APG-GUI/scores.h:4,
                 from ../APG-GUI/scores.cpp:1:
/opt/Qt/5.3/gcc_64/include/QtWidgets/qdialog.h:93:17: note: virtual int QDialog::exec()
     virtual int exec();
                 ^
/opt/Qt/5.3/gcc_64/include/QtWidgets/qdialog.h:93:17: note:   candidate expects 0 arguments, 1 provided
../APG-GUI/scores.cpp:40:48: error: 'qstr' was not declared in this scope
     ui->plainTextEdit->setPlainText(qstr(result));

我完全不知道那个故障的原因。为什么我不能使用=+算子?这是c++中内置的!我所有的东西(我想我已经)都进行了适当的申报和检查。我是Qt的初学者,所以也许我做错了什么。我在网上寻找解决方案,但是,不幸的是,没有找到任何符合我问题的东西。下面是我使用的标题和变量声明:

#include "scores.h"
#include "cstdio"
#include "ui_scores.h"
#include "cstdlib"
#include "iostream"
#include "string"
int n,m,sx,a;
QString mode, exclude;

我的构造函数代码(包括"坏"行):

Scores::Scores(QWidget *parent, int nk, int mk, int xk, int ak, QString modesk, QString excludek) :
    QDialog(parent),
    ui(new Ui::Scores)
{
    n = nk;
    m = mk;
    a = ak;
    mode = modesk;
    sx = xk;
    exclude = excludek;
    ui->setupUi(this);
    std::string wyniki = std::string("apg -q -n ") + n + " -m " + m + " -x " + sx + " -a " + a; //badline
    if(exclude != "") wyniki+=" -E " + exclude.toUtf8().constData(); //badline
    if(a==1)wyniki += " -M " + mode; //badline 
    std::string result = exec(wyniki.c_str()); //badline
    ui->plainTextEdit->setPlainText(qstr(result));
}

使用QString作为主字符串类型:

QString result = QString("apg -q -n %1 -x %2 -y %3").arg(n).arg(x).arg(y);

或者用QTextStream来组装一切

QString result;
QTextStream ts(&result);
ts << "apg -q -n " << n << " -x " << x;

使用std::string.arg():

std::string x = "xxx";
QString result = QString("xxx -x %1").arg(x.c_str());

有关QStringQTextStream的详细信息,请参阅Qt文档

替换下面的语句

std::string wyniki = "apg -q -n " + n + " -m " + m + " -x " + sx + " -a " + a;

std::string wyniki = std::string( "apg -q -n " ) + n + " -m " + m + " -x " + sx + " -a " + a;

前提是所有操作数的类型为char[]或char *。

否则,例如,如果变量n是int类型,则写入

std::string wyniki = "apg -q -n " + std::to_string( n ) + " -m " + m + " -x " + sx + " -a " + a;

注意只有std::string类型的对象才重载操作符+。

在你更新了你的帖子之后,你的"坏行"必须看起来像

std::string wyniki = "apg -q -n " + std::to_string( n ) + " -m " + 
                     std::to_string( m ) + " -x " + 
                     std::to_string( sx ) + " -a " + std::to_string( a );

如果QString有到const char *的隐式转换操作符,则不用

if(a==1)wyniki += " -M " + mode; //badline 

你必须写

if(a==1)wyniki += std::string( " -M " ) + mode; 

相关内容

  • 没有找到相关文章

最新更新