std::string Arguments in Subprogram Calls

M

Mike Copeland

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
 
A

Alf P. Steinbach

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
 
C

Christopher

   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)
 
J

Jorgen Grahn

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
 
G

Goran

   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.
 
M

Mike Copeland

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
 
M

Mike Copeland

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
 
A

Alf P. Steinbach

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
 
M

Mike Copeland

"'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<}}
 
J

Joe Greer

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top