类中的 C++ 字符串

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

C++ string in classes

c++stringcompiler-errors

提问by Kim Gr?sman

I know this is quite a ridiculous question but this is quite confusing and irritating, as something that shouldwork simply is not. I'm using Code Blocks with the GCC compiler and I am trying to simply create a string variable in my class

我知道这是一个非常荒谬的问题,但这是非常令人困惑和恼人的,因为本来应该起作用的东西却没有。我在 GCC 编译器中使用代码块,我试图在我的类中简单地创建一个字符串变量

#ifndef ALIEN_LANGUAGE
#define ALIEN_LANGUAGE

#include <string>

class Language
{
    public:

    private:
        string str;
};

#endif

Strange enough, my compiler halts me with an error saying this:

奇怪的是,我的编译器以这样的错误阻止了我:

C:\Documents and Settings\...|11|error: `string' does not name a type|
||=== Build finished: 1 errors, 0 warnings ===|

For some reason, it is unable to find the class "string" which for some reason, my main.cpp is able to detect "#include " while my language class is not able for some reason.

出于某种原因,它无法找到类“string”,出于某种原因,我的 main.cpp 能够检测到“#include”,而我的语言类由于某种原因无法检测到。

This is the main I wrote quickly just to see it main itself is able to see the string file:

这是我快速写的 main 只是为了看到它 main 本身能够看到字符串文件:

//main.cpp

#include <iostream>
#include <string>
#include "alien_language.h"

using namespace std;

int main()
{
    string str;

    return 0;
}

Does anyone know what's going on?

有谁知道发生了什么?

回答by GManNickG

using namespace std;

using namespace std;

That's what's going on.

这就是正在发生的事情。

You don't have std::prefixing the string in your class. Everything in the standard library is in the namespace std.

您没有std::在类中为字符串添加前缀。标准库中的所有内容都在命名空间中std

It is generally regarded as bad practice to use using namespace std;, by the way. For more information on why and what to do instead, check out this question: Using std Namespace.

using namespace std;顺便说一句,通常认为使用 是不好的做法。有关原因和操作的更多信息,请查看此问题:使用 std 命名空间

回答by Ropez

The string class is defined in the std namespace. You should chenge the class to this:

字符串类在 std 命名空间中定义。你应该把班级改成这样:

class Language
{
    public:

    private:
        std::string str;
};

It is also possible, but not recommended to add this to the top of the header file:

也可以,但不建议将其添加到头文件的顶部:

using namespace std;

回答by Kim Gr?sman

string is in namespace std, and you need to qualify it fully inside your header file:

字符串在命名空间 std 中,您需要在头文件中完全限定它:

#include <string>

class Language
{
    public:

    private:
        std::string str;
};

Do not use using namespace std;or similar in header files.

不要using namespace std;在头文件中使用或类似。

回答by Silfverstrom

You should refer to it as std::string;

您应该将其称为 std::string;

回答by Twisol

It looks to me like you're missing the all-important (with a hint of sarcasm) using namespace std;line. Either add that in before your class, or explicitely use std::string str. I'd recommend against adding the using namespace std;line in a header file, as it would pollute the mainspace for any file that includes it.

在我看来,您似乎错过了最重要的(带有一丝讽刺意味)的using namespace std;台词。要么在您的课程之前添加它,要么明确使用std::string str. 我建议不要using namespace std;在头文件中添加该行,因为它会污染包含它的任何文件的主空间。

回答by Abhay

The stringclass in standard C++ is in stdnamespace. Write something like using std::string;in your header or fully qualify it as std::string in your header.

string标准 C++ 中的类位于std命名空间中。using std::string;在你的标题中写一些类似的东西或者 在你的标题中将它完全限定为 std::string 。

Beware that using namespace std;in header is a bad practice (read here).

请注意,using namespace std;在标题中是一种不好的做法(请阅读此处)。