0

这是我在 .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 + " ";
}

也可以。

有人可以向我解释一下吗?我是否缺少一些字符串的基本知识?

谢谢。

4

2 回答 2

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 + " ");
}
于 2013-07-12T11:56:48.670 回答
0

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

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

ReturnType NameOfTheFunction(Parameters)
{
    Implementation
}
于 2013-07-12T12:00:17.890 回答