C++ <string> 在头文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10387630/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:00:12  来源:igfitidea点击:

<string> in header file

c++stringheader

提问by Bogdan Maier

I`m trying to define a variable of string type in a class definition in a header file. Is it possible? Example:

我正在尝试在头文件的类定义中定义字符串类型的变量。是否可以?例子:

/* Foo.h */   
#include <string>
class Foobar{
     int a;
     string foo;

}

Because somehow in main I can declare a string variable, but in the header it doesn't recognize my string type.

因为不知何故,我可以在 main 中声明一个字符串变量,但在标题中它无法识别我的字符串类型。

回答by ildjarn

stringlives in namespace std. Make that:

string住在命名空间中std。做到这一点:

#include <string>

class Foobar {
    int a;
    std::string foo;
};