call of overloaded replace(char,char, QT::CaseSensitivity) is ambiguous



  • Hi, the statement:
    tokFormula = tokFormula.replace('+','-',Qt::CaseInsensitive);

    I got a warning:
    call of overloaded replace(char,char, QT::CaseSensitivity) is ambiguous.
    If I change the statement to:
    tokFormula = tokFormula.replace("+","-",Qt::CaseInsensitive);
    then it works well.

    But in QT document:
    QString &QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
    This function overloads replace().
    Replaces every occurrence of the character before with the character after and returns a reference to this string.



  • Hi @samzhcs1,

    call of overloaded replace(char,char, QT::CaseSensitivity) is ambiguous.

    At a glance, I'd say the reason this is ambiguous is because there is no QString::replace overload that takes char - you're probably expecting the overload that takes QChar (which is not char) but the compiler cannot make that assumption.

    Put another way, the compiler could just as validly turn your char arguments into QChar or QString - obviously we can tell from the names that you probably want QChar, but the compiler doesn't know this.

    So, try converting your char argument to QChar explicitly, like:

    myString.replace(QChar(char1), QChar(char2), ...);
    

    Cheers.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.