Question

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.

Was it helpful?

Solution

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 + " ");
}

OTHER TIPS

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top