Void return empty QString [SOLVED]



  • Hi,

    I am trying to write a void that should returns several QString, in the void everything looks fine, I see what I what, but once I am back in calling void, all my QString are empty. I don't know what I am doing wrong:

    @void GetMolInfoFromSDF(QString file, QString SMILES, QString ChemicalFormula){
    ROMol *mol=MolFileToMol(qPrintable(file));
    ChemicalFormula=Descriptors::calcMolFormula(*mol).c_str();
    SMILES=MolToSmiles(*mol).c_str();
    cout<<qPrintable(SMILES)<<" "<<qPrintable(ChemicalFormula)<<endl;
    return;}@

    The main is just:
    @QString file="mol.sdf";
    QString SMILES;
    QString ChemicalFornula;
    GetMolInfoFromSDF( file, SMILES, ChemicalFormula);
    cout<<qPrintable(SMILES)<<" "<<qPrintable(ChemicalFormula)<<endl;
    @

    cout in void is just fine, cout in main returns empty value.
    What is wrong?

    Thanks

    Laƫtitia


  • Moderators

    You are passing a copy of the strings to the method (call-by-value). But since you want to alter the passed parameters inside your method you need to pass references or pointers (call-by-reference).

    So your method signature should look like this:
    @
    void GetMolInfoFromSDF(QString & file, QString & SMILES, QString & ChemicalFormula)
    {
    ...
    }
    @



  • The parameters of your function are values, not references. That means that inside your function, you are working on a copy of the values you passed in, not on the actual values.

    Use references if you want to manipulate the values themselves:
    @
    void GetMolInfoFromSDF(QString file, QString& SMILES, QString ChemicalFormula&) {
    @

    However, I usually find it better to use the return value instead of pushing returns into function parameters. In this case, you'd have to return something that can contain both pieces of data.



  • Thanks, I always messing up with * and &. Still having problem to get the logic. Anyway now it works.


Log in to reply
 

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