std::string Arguments in Subprogram Calls

Discussion in 'C++' started by Mike Copeland, Dec 20, 2011.

  1. How can I use std::string parameters (as return data) in subprogram
    calls? In the following routine, I have 1 input and 3 output arguments:

    void getPhaseData(int pNum, string X, string Y, short *T)
    {
    switch (pNum)
    {
    case 0:
    {
    X = "Null Phase", Y = "", *T = NPTyp;
    break;
    }
    case 1:
    {
    X = "Run Finish", Y = " Run", *T = FPTyp;
    break;
    }
    case 2:
    {
    X = "Bike Finish", Y = "Bike", *T = FPTyp;
    break;
    }
    [etc.]
    }
    return;
    } // getPhase_Data

    This doesn't work, because the 2 string arguments are returned as
    NULL. The problem has occurred as I try to convert very old code (20+
    years) to more modern C++ code, using STL containers and such. The old
    code used C strings and pointers, but I can't get this code to work.
    Please advise. TIA
     
    Mike Copeland, Dec 20, 2011
    #1
    1. Advertising

  2. On 20.12.2011 18:54, Mike Copeland wrote:
    > How can I use std::string parameters (as return data) in subprogram
    > calls? In the following routine, I have 1 input and 3 output arguments:
    >
    > void getPhaseData(int pNum, string X, string Y, short *T)
    > {
    > switch (pNum)
    > {
    > case 0:
    > {
    > X = "Null Phase", Y = "", *T = NPTyp;
    > break;
    > }
    > case 1:
    > {
    > X = "Run Finish", Y = " Run", *T = FPTyp;
    > break;
    > }
    > case 2:
    > {
    > X = "Bike Finish", Y = "Bike", *T = FPTyp;
    > break;
    > }
    > [etc.]
    > }
    > return;
    > } // getPhase_Data
    >
    > This doesn't work, because the 2 string arguments are returned as
    > NULL. The problem has occurred as I try to convert very old code (20+
    > years) to more modern C++ code, using STL containers and such. The old
    > code used C strings and pointers, but I can't get this code to work.
    > Please advise. TIA


    struct PhaseData
    {
    string x;
    string y;
    int t;
    };

    PhaseData phaseData( int i )
    {
    static PhaseData const data[] =
    {
    { "Null Phase", "", NPTyp },
    { "Run Finish", " Run", FPTyp },
    { "Bike Finish", "Bike", FPType }
    };

    assert( i < 3 );
    return data;
    }



    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Dec 20, 2011
    #2
    1. Advertising

  3. Mike Copeland

    Christopher Guest

    On Dec 20, 11:54 am, (Mike Copeland) wrote:
    >    How can I use std::string parameters (as return data) in subprogram
    > calls?  In the following routine, I have 1 input and 3 output arguments:


    What is the formal definition of a "Subprogram" ?

    Use references or pointers:
    void getPhaseData(int num, const std::string & x, const std::string &
    y, short * t)
     
    Christopher, Dec 20, 2011
    #3
  4. Mike Copeland

    Jorgen Grahn Guest

    On Tue, 2011-12-20, Mike Copeland wrote:
    > How can I use std::string parameters (as return data) in subprogram
    > calls? In the following routine, I have 1 input and 3 output arguments:
    >
    > void getPhaseData(int pNum, string X, string Y, short *T)
    > {
    > switch (pNum)
    > {
    > case 0:
    > {
    > X = "Null Phase", Y = "", *T = NPTyp;
    > break;
    > }

    ....
    > }
    > return;
    > } // getPhase_Data
    >
    > This doesn't work, because the 2 string arguments are returned as
    > NULL. The problem has occurred as I try to convert very old code (20+
    > years) to more modern C++ code, using STL containers and such.


    To be blunt, you need to learn some C++ first. You seem to think C++
    uses call-by-reference and that a string can be NULL. Chances are you
    have other misconceptions too which prevent you from writing working
    code.

    (What language do you come from, by the way? I don't recognize the
    term "subprogram". Google suggests Fortran or Ada.)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Dec 20, 2011
    #4
  5. Mike Copeland

    Goran Guest

    On Dec 20, 6:54 pm, (Mike Copeland) wrote:
    >    How can I use std::string parameters (as return data) in subprogram
    > calls?  In the following routine, I have 1 input and 3 output arguments:
    >
    > void getPhaseData(int pNum, string X, string Y, short *T)
    > {
    >    switch (pNum)
    >    {
    >       case 0:
    >       {
    >         X = "Null Phase",  Y = "", *T = NPTyp;
    >         break;
    >       }
    >       case 1:
    >       {
    >         X = "Run Finish",  Y = " Run", *T = FPTyp;
    >         break;
    >       }
    >       case 2:
    >       {
    >         X = "Bike Finish", Y = "Bike", *T = FPTyp;
    >         break;
    >       }
    > [etc.]
    >    }
    >         return;
    >
    > }  // getPhase_Data
    >
    >    This doesn't work, because the 2 string arguments are returned as
    > NULL.  The problem has occurred as I try to convert very old code (20+
    > years) to more modern C++ code, using STL containers and such.  The old
    > code used C strings and pointers, but I can't get this code to work.


    +1 for what Alf did, it's arguably the best practice for what you
    need. But I am guessing that doing it would require other changes to
    your code.

    That said, you did "short* T" and "*T = FPTyp". By analogy, you should
    have done the same with your strings, why didn't you? ;-)

    Anyhow... Here, if you decide not to use Alf's solution, you need pass-
    by-reference. As a reminder: C language can only pass parameters by
    value. So in C, you emulate pass-by-reference using pointers (that's
    your short* T). You can do that in C++ as well, but C++ also supports
    actual pass-by-reference. That leads you to what Leigh did.

    Goran.
     
    Goran, Dec 21, 2011
    #5
  6. In article <jcqinp$jh9$>,
    says...
    > > How can I use std::string parameters (as return data) in subprogram
    > > calls? In the following routine, I have 1 input and 3 output arguments:
    > > void getPhaseData(int pNum, string X, string Y, short *T)
    > > {
    > > switch (pNum)
    > > {
    > > case 0:
    > > {
    > > X = "Null Phase", Y = "", *T = NPTyp;
    > > break;
    > > }
    > > case 1:
    > > {
    > > X = "Run Finish", Y = " Run", *T = FPTyp;
    > > break;
    > > }
    > > case 2:
    > > {
    > > X = "Bike Finish", Y = "Bike", *T = FPTyp;
    > > break;
    > > }
    > > [etc.]
    > > }
    > > return;
    > > } // getPhase_Data
    > >
    > > This doesn't work, because the 2 string arguments are returned as
    > > NULL. The problem has occurred as I try to convert very old code (20+
    > > years) to more modern C++ code, using STL containers and such. The old
    > > code used C strings and pointers, but I can't get this code to work.
    > > Please advise. TIA

    >
    > struct PhaseData
    > {
    > string x;
    > string y;
    > int t;
    > };
    >
    > PhaseData phaseData( int i )
    > {
    > static PhaseData const data[] =
    > {
    > { "Null Phase", "", NPTyp },
    > { "Run Finish", " Run", FPTyp },
    > { "Bike Finish", "Bike", FPType }
    > };
    >

    Unfortunately, this code won't compile. 8<{{ The compile errors
    say:

    "'initializing' : cannot convert from 'char [11]' to 'const struct
    PhaseData'"

    There is no mention of "char[11]" in the code, so the error is very
    confusing. What did I do wrong? Please advise. TIA

    struct PhaseData // per Alf P. Steinbach 12/20/2011
    {
    string x;
    string y;
    int t;
    };
    PhaseData phaseData(int i)// fetch Phase Information
    {
    static PhaseData const data[]=
    {
    {"Null Phase", "", NPTyp},
    {"Run Finish", " Run", FPTyp},
    {"Bike Finish", "Bike", FPTyp},
    {"Swim Finish", "Swim", FPTyp},
    {"Swim->Bike Transition", "S->B", NPTyp},
    {"Bike->Run Transition", "B->R", NPTyp},
    {"Swim->Run Transition", "S->R", NPTyp},
    {"Bike->Swim Transition", "B->S", NPTyp},
    {"Run->Bike Transition", "R->B", NPTyp},
    {"Skate", "Skate", NPTyp},
    {"Error - Restart", "Error ", RSTyp},
    {"Done", "Error ", EDTyp}
    }
    assert(i < 12);
    return data;
    } // phaseData
     
    Mike Copeland, Dec 28, 2011
    #6
  7. In article <jcqinp$jh9$>,
    says...
    > On 20.12.2011 18:54, Mike Copeland wrote:
    > > How can I use std::string parameters (as return data) in subprogram
    > > calls? In the following routine, I have 1 input and 3 output arguments:
    > >
    > > void getPhaseData(int pNum, string X, string Y, short *T)
    > > {
    > > switch (pNum)
    > > {
    > > case 0:
    > > {
    > > X = "Null Phase", Y = "", *T = NPTyp;
    > > break;
    > > }
    > > case 1:
    > > {
    > > X = "Run Finish", Y = " Run", *T = FPTyp;
    > > break;
    > > }
    > > case 2:
    > > {
    > > X = "Bike Finish", Y = "Bike", *T = FPTyp;
    > > break;
    > > }
    > > [etc.]
    > > }
    > > return;
    > > } // getPhase_Data
    > >
    > > This doesn't work, because the 2 string arguments are returned as
    > > NULL. The problem has occurred as I try to convert very old code (20+
    > > years) to more modern C++ code, using STL containers and such. The old
    > > code used C strings and pointers, but I can't get this code to work.
    > > Please advise. TIA

    >
    > struct PhaseData
    > {
    > string x;
    > string y;
    > int t;
    > };
    >
    > PhaseData phaseData( int i )
    > {
    > static PhaseData const data[] =
    > {
    > { "Null Phase", "", NPTyp },
    > { "Run Finish", " Run", FPTyp },
    > { "Bike Finish", "Bike", FPType }
    > };
    >

    Unfortunately, this code won't compile. 8<{{ The compile errors
    say:

    "'initializing' : cannot convert from 'char [11]' to 'const struct
    PhaseData'"

    There is no mention of "char[11]" in the code, so the error is very
    confusing. What did I do wrong? Please advise. TIA

    struct PhaseData
    {
    string x;
    string y;
    int t;
    };
    PhaseData phaseData(int I)// fetch Phase Information
    {
    static PhaseData const data[]=
    {
    {"Null Phase", "", NPTyp},
    {"Run Finish", " Run", FPTyp},
    {"Bike Finish", "Bike", FPTyp},
    {"Swim Finish", "Swim", FPTyp},
    {"Swim->Bike Transition", "S->B", NPTyp},
    {"Bike->Run Transition", "B->R", NPTyp},
    {"Swim->Run Transition", "S->R", NPTyp},
    {"Bike->Swim Transition", "B->S", NPTyp},
    {"Run->Bike Transition", "R->B", NPTyp},
    {"Skate", "Skate", NPTyp},
    {"Error - Restart", "Error ", RSTyp},
    {"Done", "Error ", EDTyp}
    }
    assert(I < 12);
    return data;
    } // phaseData
     
    Mike Copeland, Dec 28, 2011
    #7
  8. On 28.12.2011 04:51, Mike Copeland wrote:
    > Unfortunately, this code won't compile. 8<{{ The compile errors
    > say:
    >
    > "'initializing' : cannot convert from 'char [11]' to 'const struct
    > PhaseData'"
    >
    > There is no mention of "char[11]" in the code, so the error is very
    > confusing. What did I do wrong? Please advise. TIA
    >
    > struct PhaseData
    > {
    > string x;
    > string y;
    > int t;
    > };
    > PhaseData phaseData(int I)// fetch Phase Information
    > {
    > static PhaseData const data[]=
    > {
    > {"Null Phase", "", NPTyp},
    > {"Run Finish", " Run", FPTyp},
    > {"Bike Finish", "Bike", FPTyp},
    > {"Swim Finish", "Swim", FPTyp},
    > {"Swim->Bike Transition", "S->B", NPTyp},
    > {"Bike->Run Transition", "B->R", NPTyp},
    > {"Swim->Run Transition", "S->R", NPTyp},
    > {"Bike->Swim Transition", "B->S", NPTyp},
    > {"Run->Bike Transition", "R->B", NPTyp},
    > {"Skate", "Skate", NPTyp},
    > {"Error - Restart", "Error ", RSTyp},
    > {"Done", "Error ", EDTyp}
    > }


    Here ^ is a missing semicolon.


    > assert(I< 12);
    > return data;
    > } // phaseData



    Other than that it compiles with Visual C++ 10.0 and MinGW g++ 4.4.1.

    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Dec 28, 2011
    #8
  9. In article <jde668$cjb$>,
    says...
    > > "'initializing' : cannot convert from 'char [11]' to 'const struct
    > > PhaseData'"
    > > There is no mention of "char[11]" in the code, so the error is very
    > > confusing. What did I do wrong? Please advise. TIA
    > > struct PhaseData
    > > {
    > > string x;
    > > string y;
    > > int t;
    > > };
    > > PhaseData phaseData(int I)// fetch Phase Information
    > > {
    > > static PhaseData const data[]=
    > > {
    > > {"Null Phase", "", NPTyp},
    > > {"Run Finish", " Run", FPTyp},
    > > {"Bike Finish", "Bike", FPTyp},
    > > {"Swim Finish", "Swim", FPTyp},
    > > {"Swim->Bike Transition", "S->B", NPTyp},
    > > {"Bike->Run Transition", "B->R", NPTyp},
    > > {"Swim->Run Transition", "S->R", NPTyp},
    > > {"Bike->Swim Transition", "B->S", NPTyp},
    > > {"Run->Bike Transition", "R->B", NPTyp},
    > > {"Skate", "Skate", NPTyp},
    > > {"Error - Restart", "Error ", RSTyp},
    > > {"Done", "Error ", EDTyp}
    > > }

    > Here ^ is a missing semicolon.
    > > assert(I< 12);
    > > return data;
    > > } // phaseData

    >
    > Other than that it compiles with Visual C++ 10.0 and MinGW g++ 4.4.1.


    This doesn't fix the compile errors in my compiler (VS6.0...which I
    know is archaic and obsolete). Guess I'll have to find another way to
    do all this. <sigh>
    Thanks for your efforts, Alf. 8<}}
     
    Mike Copeland, Dec 28, 2011
    #9
  10. Mike Copeland

    Joe Greer Guest

    You can pretty easily drop back to C in this case and things will work
    about as well. Make the "strings" "char *" and move on. Since you are
    initializing them with constants, you don't even have to worry about
    freeing the pointers later. If you really need the fields to be strings,
    then you will have to do something like create an array of pointers and
    initialize that at compile time and then iterate through the list and
    assign the strings at some dynamic initialization time.

    hih,
    joe
     
    Joe Greer, Dec 28, 2011
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Jones

    Hiding of subprogram designators

    David Jones, Nov 17, 2003, in forum: VHDL
    Replies:
    1
    Views:
    568
    Alan Fitch
    Nov 18, 2003
  2. Taras_96
    Replies:
    2
    Views:
    4,896
    Taras_96
    Aug 3, 2005
  3. Peter Jansson
    Replies:
    5
    Views:
    6,315
    Ivan Vecerina
    Mar 17, 2005
  4. Fei Liu
    Replies:
    9
    Views:
    447
  5. Jeffrey Walton
    Replies:
    10
    Views:
    942
    Mathias Gaunard
    Nov 26, 2006
Loading...

Share This Page