返回字符串;没有花括号就无法工作
return string; not working without 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 + " ";
}
同样有效。
有人能给我解释一下吗?我是不是错过了一些弦的基本知识?
谢谢。
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 + " ");
}
这与字符串无关。这是关于你如何定义你的功能。在这种情况下,ReturnString
是一个返回字符串的函数。
C++函数定义的通用格式为:
ReturnType NameOfTheFunction(Parameters)
{
Implementation
}
相关文章:
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 在 c++ 中对字符串进行操作的关系运算符无法按预期工作
- C++ "noskipws"没有按预期工作,如何正确允许字符串中的空格?
- C++20字符串文本模板参数工作示例
- 将双精度值转换为 char 变量时字符串流如何工作
- 为什么在C++中使用关系运算符创建的模板函数不能对字符串正常工作?
- 为什么 C 样式字符串的工作空指针检查不?
- 比较字符串的 GetLine 工作一次,然后比较之后不起作用
- 在 stl 中的字符串中擦除关键字的工作?
- scanf() 语句中"%*[^n]"的格式字符串指示什么?分配抑制器 (*) 和否定扫描集 ([^) 如何协同工作?
- 使用 for 循环打印字符串数组无法正常工作 (C++)
- 字符串文字到 char 数组的转换如何在C++中实际工作
- 如何让我的构造函数和函数工作,以便我的 main() 能够同时显示字符串和 int 数据
- 如何使用字符串更改此插入功能以在链接列表中工作
- 如何与C 字符串中的非ASCII字符一起工作
- 读取进程内存无法正常工作,使用 UTF16 字符串
- strcpy() 和 strcat() 在 Turbo C++ 中无法正常工作(从字符串 litteral 初始化数组时
- C-字符串的获取线函数无法正常工作
- 这个右移是如何工作的:字符串流>>无符号的int>>无符号的int?
- 字符串的二进制搜索无法正常工作