2013-06-28 178 views
0

我想在C++ Eclipse來寫一個Qt程序,但我不能讓過去的錯誤:無效參數

void MyTests::populateFirstList(){ 
    Question* q = new Question; 
    q = this->ctr->getCurrent(); 
    string s = this->ctr->toString(q); 
} 

問題是我定義的類型,以及帶有toString(q)的行會返回一個錯誤,指出無效參數。 的funcion的toString():

string Controller::toString(Question* q){ 
    string s=""; 
    string text = q->getText(); 
    char c; 
    string::iterator it; 
    for (it= text.begin(); it != text.end(); it++) 
    { 
     if ((*it) == ' ') { 
      s+="\n"; 
     } 
     else { 
      s+=it; 
     } 
    } 
    return s; 
} 

而只是爲了安全起見,功能getCurrent():

Question* Controller::getCurrent(){ 
    return this->question; 
} 

我不明白爲什麼會這樣,因爲函數的toString()應一個指向問題的指針,q是一個。我甚至不確定這些錯誤是由這些函數還是在更深的地方引起的。謝謝你的幫助。

的錯誤信息是:

invalid arguments ' Candidates are: 
    std::basic_string < char,std::char_traits <char>, std::allocator <char> > 
     toString(Question *) ' 
+0

1.什麼是錯誤? 2.你用'Question * q = new Question;'分配一個內存,下一行殺死一個指針:'q = this-> ctr-> getCurrent();' – borisbn

+0

**確切**錯誤信息,請! –

+0

@borisbn我不確定第一個問題應該是什麼意思,至於第二個,q是一個指針,通過這個函數,不是嗎?其實我對指針很困惑。 – user1796659

回答

0

最終,你的錯誤來自行

 } else s+=it; 

,最終應該是

 } else s+=*it; 

這可能不會解決問題,但我在這裏沒有看到任何QT。編寫Qt-App時爲什麼不使用QT-Objects?

void MyTests::populateFirstList(){ 
    Question* q = this->ctr->getCurrent(); 
    QString s = this->ctr->toString(q); 
} 


QString Controller::toString(Question* q){ 
    QString s; 
    QString text = q->getText(); 
    s = text.replace(" ","\\n"); 
    return s; 
} 


Question* Controller::getCurrent(){ 
    return this->question; 
} 
+1

這並沒有解決它。另外,我對Qt非常不熟悉,並且希望儘量減少它的使用。 :) – user1796659

+0

我發現了2個引用到你的錯誤,其中一個是缺少eclipse的STL配置,另一個是CDT解析器中的缺陷。只是從你的代碼中,鑑於類型是正確的,這看起來不錯。你可以嘗試通過禁用錯誤分析器和編譯/運行,看看它是否工作。但一般來說,如果你想與QT合作,不要避免它:)。我不喜歡混合到許多框架的想法,所以我只在QT上工作。 –

1

執行:運行QMAKE 再建 :)

+0

仍然有問題。我正在嘗試爲Android構建現有的QT Windows應用程序。 –