【问题标题】:Error while using += operator in Qt在 Qt 中使用 += 运算符时出错
【发布时间】:2014-08-25 17:21:14
【问题描述】:

我在 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));
}

【问题讨论】:

  • 它不是C++内置的,而是标准库,但需要提供SSCCE。什么是nm 等?
  • 仔细阅读错误信息。他们正在告诉你到底发生了什么。
  • @FinalContest 我给了你我的变量列表
  • @Ty221:我的意思是给出一个不言自明的代码,可以编译而不是逆序的块等等。
  • 你将 QString 与 std::string 甚至 QByteArray 混合在一起,难怪你会得到这个错误。为什么不坚持一个并在最后一步将所有内容转换为正确的格式。

标签: c++ qt compiler-errors stdstring qstring


【解决方案1】:

替换此语句

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; 

【讨论】:

  • 这是错误的答案,因为 int 仍然存在而没有转换。
  • 是的,在我的帖子之后,当然,我什至很感激......但是,即使那样,这个答案也没有解决所有的坏行,而且一开始的一切都是无效的 -鲱鱼。
  • @Ty221 见我的帖子。我展示了如何改变“坏线”,
  • 1) 是的,我知道。我之前也提供了解决方案,但决定删除。 :) 2) 我不喜欢std::,对不起。重复很多次是愚蠢的。 3)我认为这个问题不应该得到任何帮助。 4)您仍然没有解决所有“坏”行。
【解决方案2】:

使用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。可以正常使用。
  • @FinalContest 当然你可以使用std::string,但他正在编写一个Qt应用程序,所以他不应该使用std::string来组装结果。他应该改用QString
  • 这是为什么呢?因为你喜欢QString?除了你的喜好,他可能不喜欢别的东西?
  • 事实上,我有时也更喜欢 std::string 而不是 QString,因为它不那么臃肿,不需要 Qt 应用程序实例等等。
  • 或者,换种说法:我不知道std::string 打算解决什么问题,但它肯定不是我真正需要的东西。而QString 的 API 似乎在某种程度上是由人们在现实生活中的需求所指导的,而不是坐在标准化委员会时 :)
猜你喜欢
  • 2013-10-14
  • 1970-01-01
  • 2021-11-09
  • 2018-09-13
  • 1970-01-01
  • 2019-07-31
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多