0

This is the function I have in a .h file:

static std::string ReturnString(std::string some_string)
    return ("\t<" + some_string + " ");

The compiler (g++ -std=c++0x -pedantic -Wall -Wextra) throws these errors:

error:expected identifier before '(' token
error:named return values are no longer supported
error:expected '{' at end of input
warning: no return statement in function returning non-void [-Wreturn-type]

But,

static std::string ReturnString(std::string some_string)
{
    return ("\t<" + some_string + " ");
}

works fine. Even,

static std::string ReturnString(std::string some_string)
{
    return "\t<" + some_string + " ";
}

works too.

Could someone please explain this to me please? Am I missing some basic knowledge of strings?

Thanks.

sunam
  • 181
  • 3
  • 8
  • 6
    Just basic knowledge of functions : you always have to enclose a function's body between curly braces, even when it's only one line long. And that's about it. – Nbr44 Jul 12 '13 at 11:52

2 Answers2

1
static std::string ReturnString(std::string some_string)
    return ("\t<" + some_string + " ");

It's actually a basic knowledge of C++ that you're missing. In C++, function bodies must be enclosed in curly braces, {}. So, the correct definition for the function above would be:

static std::string ReturnString(std::string some_string)
{
    return ("\t<" + some_string + " ");
}
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Ah! Thanks. Embarrassed. Have done one line functions before in the same line with {} and just an enter into second line made me forget the brackets. – sunam Jul 12 '13 at 12:00
0

This has nothing to do with strings. This is about how you have defined your function. In this case, ReturnString is a function which returns a string.

General format for C++ function definition is:

ReturnType NameOfTheFunction(Parameters)
{
    Implementation
}
Varun
  • 691
  • 4
  • 9