how do I create a function that returns a string? I'm not sure how to declare it in the file header, as coding "string function(int x)" does not work.
how do I create a function that returns a string? I'm not sure how to declare it in the file header, as coding "string function(int x)" does not work.
Yeah, tried that too, but I get an error when compliling.
" 'std' : is not a class or namespace name"
Do I need to add an include statement?
what is the proper syntax in calling this function and returning a string to the main program? do i need to send the string into the function as a variable?
Code:string=function(x); function(x){ return(?); }
Why would you need to pass it in? You just need to return something that is of proper type.
Example:
Code:#include <string> #include <iostream> std::string foo(int x) { switch (x) { case 0: return "Hello"; case 1: return "World!"; default: return "Bad input"; } } int main() { std::cout << foo(0) << " " << foo(1) << std::endl; }
i'm getting an error:
"error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall COddsDlg::GetOdds(double)" (?GetOdds@COddsDlg@@QAE?AV?$basic_string@DU?$char_ [email protected] unresolved externals"
this is what i have:
in header:
in Main:Code:std::string odds; std::string GetOdds(double percentage);
any idea why?Code:odds=GetOdds(percentage); ... std::string GetOdds(double percentage){ std::string converted_odds; std::stringstream ss; double temp_Odds; temp_Odds=(1.0/percentage)-1.0; ss << std::fixed << std::setprecision(1) << percentage*100.0; converted_odds=ss.str(); return converted_odds; }
Let me guess. You have something like:"error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall COddsDlg::GetOdds(double)" (?GetOdds@COddsDlg@@QAE?AV?$basic_string@DU?$char_ [email protected] unresolved externals"
Right?Code:class COddsDlg { //... std::string GetOdds(double percentage); // ... } std::string GetOdds(double percentage){ // ... }
See the declaration is inside the class? The linker is trying to find it, but it doesn't exist, because the function you defined is global.
You have to tell the compiler that it's actually part of the class:
Code:std::string COddsDlg::GetOdds(double percentage){
how can i clear odds each time? i tried the following and neither worked:
odds = NULL;
odds.clear();
Likely you're trying to clear a local copy of odds, instead of the version that's in side the class.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
If I'm going to return a string from a class method I normally ensure that string is a private or protected member of the class. Then I can return a const reference to the string or I can return the const char * equivalent of the string (string::c_str()) since the string will not go out of scope in the function call. If not then you can return std::string and short of being in a time critical render loop you probably won't have any issues with it.