c++中如何让函数返回字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3792923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make function return string in c++
提问by node ninja
Possible Duplicate:
How to convert this code to use string
可能重复:
如何将此代码转换为使用字符串
I have a function like this:
我有一个这样的功能:
char *foo()
{
}
How can I make it return a string instead? I tried
我怎样才能让它返回一个字符串呢?我试过
string foo()
{
}
but the compiler complains.
但编译器抱怨。
回答by Benoit
Did you do this:
你做了这个了吗:
#include <string>
using std::string;
And additionnally, do you use gcc
or g++
? Even if gcc
now can compile C++ code, it is advised to use g++.
此外,您使用gcc
或g++
吗?即使gcc
现在可以编译C++代码,也建议使用g++。
回答by Nikko
Try std::string. Standard features are in the std:: namespace.
试试 std::string。标准功能位于 std:: 命名空间中。
std::string foo()
{
}
Be careful with "using" directives, especially in header files. Better take the habit to use std::
小心“使用”指令,尤其是在头文件中。最好养成使用 std:: 的习惯