返回字符串;没有花括号不工作

时间:2013-07-12 11:51:44

标签: c++ xml string curly-brackets

这是我在.h文件中的功能:

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

编译器(g ++ -std = c ++ 0x -pedantic -Wall -Wextra)会抛出这些错误:

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]

但是,

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

工作正常。 甚至,

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

也有效。

有人可以向我解释一下吗?我错过了一些基本的字符串知识吗?

感谢。

2 个答案:

答案 0 :(得分:1)

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

它实际上是您缺少的C ++基础知识。在C ++中,函数体必须用大括号{}括起来。因此,上述函数的正确定义是:

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

答案 1 :(得分:0)

这与字符串无关。这是关于如何定义函数的。在这种情况下,ReturnString是一个返回字符串的函数。

C ++函数定义的一般格式是:

ReturnType NameOfTheFunction(Parameters)
{
    Implementation
}