Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Void return empty QString [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Void return empty QString [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laetis
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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)
      {
      ...
      }
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          laetis
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved