具有Qt集成的c++中的参数无效

Invalid argument in c++ with Qt integration

本文关键字:参数 无效 c++ 集成 具有 Qt      更新时间:2023-10-16

我正试图用C++在eclipse中编写一个Qt程序,但我无法克服一个错误:

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

Question是我定义的类型,带有toString(q)的行返回一个错误,表示参数无效。函数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()应该取一个指向Question的指针,而q是一个。我甚至不确定错误是由这些函数引起的还是更深层次的原因。谢谢你的帮助。

错误消息为:

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

执行:运行QMake然后构建:)

最终您的错误来自行

        } else s+=it;

最终应该是

        } else s+=*it;

这可能不会解决问题,但我在这里看不到任何QT。为什么在编写QT应用程序时不使用QT对象?

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;
}