2014-07-04 7 views
-9

Qt Creator에서 작은 응용 프로그램을 만듭니다. 내 QDialog 생성자에서이 코드를 원했지만 작동하지 않습니다.Qt에서 + = 연산자를 사용하는 동안 오류가 발생했습니다.

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)); 
} 
+0

C++에는 기본 라이브러리가 아니지만 [SSCCE] (http://sscce.org)를 제공해야합니다. 'n','m' 등 무엇입니까? – lpapp

+1

오류 메시지를주의 깊게 읽으십시오. 그들은 당신에게 정확히 무엇이 일어나고 있는지 알려주고 있습니다. – juanchopanza

+0

@FinalContest – TN888

답변

1

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; 

.arg()std::string를 사용하려면

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

QStringQTextStream에 대한 자세한 내용은 Qt는 설명서를 참조하십시오.

+0

문제는 std :: string을 사용하는 것이 아닙니다. 제대로 사용할 수 있습니다. – lpapp

+0

@FinalContest 물론 ** std :: string을 사용할 수 있지만 Qt 애플리케이션을 작성하고 있으므로 ** std :: string을 사용하여 결과를 어셈블해서는 안됩니다. 대신'QString'을 사용해야합니다. – Flovdis

+0

왜 그런가요? QString을 좋아하기 때문에? 너의 취향이 아닌 다른 것을 선호하지 않을 수 있니? – lpapp

-1

대체이 문

에 대한

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

나는 헤더의 I 사용과 변수 선언을 게시 아래

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

모든 피연산자에는 char [] 또는 char * 유형이 있습니다. 예를 들어, 변수 n은 다음

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

는 형 표준의 객체 : 문자열 연산자 +를 오버로드를 고려 쓰기 int 형이 그렇지 않은 경우

. 당신이 다음 "나쁜 라인"했다가

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 당신이

를 작성해야하는 대신
if(a==1)wyniki += " -M " + mode; //badline 

의 다음 const char *에 암시 적 변환 연산자가있는 경우로보고 게시물을 업데이트 한 후

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

당신의 코드를 시도했지만 여전히 동일한 오류가 발생했습니다 – TN888

+0

int가 변환없이 계속 존재하기 때문에 이것은 잘못된 대답입니다. – lpapp

+0

@ Ty221 내 업데이트 된 게시물보기 –