C++ 命名空间 std 中的字符串未命名类型

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

string in namespace std does not name a type

c++stringnamespacesstd

提问by Luchian Grigore

This may just be a simple mistake that I'm not seeing, but I think I'm simply doing something wrong. Don't worry I'm not using namespace std in my header functions or anything which seemed to be this person's issue [Question I read similar to mine][1] [1]: Why am I getting string does not name a type Error?

这可能只是一个我没有看到的简单错误,但我认为我只是做错了什么。别担心,我没有在我的头函数中使用命名空间 std 或任何似乎是这个人的问题 [我读到的问题与我的相似][1] [1]:为什么我得到的字符串没有命名类型错误?

I am getting 4 errors right now:

我现在收到 4 个错误:

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.h|8|error: 'string' in namespace 'std' does not name a type|

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.h|12|error: 'string' in namespace 'std' does not name a type|

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.h|13|error: 'string' in namespace 'std' does not name a type|

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.cpp|9|error: no 'std::string Nouns::nounGenerator()' member function declared in class 'Nouns'|

||=== Build finished: 4 errors, 0 warnings ===|

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.h|8|错误:命名空间“std”中的“string”没有命名类型|

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.h|12|错误:命名空间“std”中的“string”没有命名类型|

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.h|13|错误:命名空间“std”中的“string”没有命名类型|

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.cpp|9|错误:没有在类“Nouns”中声明的“std::string Nouns::nounGenerator()”成员函数|

||=== 构建完成:4 个错误,0 个警告 ===|

Here is my header file:

这是我的头文件:

class Nouns
{
    public:
        Nouns();
        std::string noun;
    protected:
    private:
        int rnp; // random noun picker
        std::string dog, cat, rat, coat, toilet, lizard, mime, clown, barbie, pig, lamp, chair, hanger, pancake, biscut, ferret, blanket, tree, door, radio;
        std::string nounGenerator()
};

And this is my cpp file:

这是我的 cpp 文件:

#include "Nouns.h"
#include <iostream>

Nouns::Nouns()
{

}

std::string Nouns::nounGenerator(){
    RollRandom rollRandObj;

    rnp = rollRandObj.randNum;

    switch(rnp){
    case 1:
        noun = "dog";
        break;
    case 2:
        noun = "cat";
        break;
    case 3:
        noun = "rat";
        break;
    case 4:
        noun = "coat";
        break;
    case 5:
        noun = "toilet";
        break;
    case 6:
        noun = "lizard";
        break;
    case 7:
        noun = "mime";
        break;
    case 8:
        noun = "clown";
        break;
    case 9:
        noun = "barbie";
        break;
    case 10:
        noun = "pig";
        break;
    case 11:
        noun = "lamp";
        break;
    case 12:
        noun = "chair";
        break;
    case 13:
        noun = "hanger";
        break;
    case 14:
        noun = "pancake";
        break;
    case 15:
        noun = "biscut";
        break;
    case 16:
        noun = "ferret";
        break;
    case 17:
        noun = "blanket";
        break;
    case 18:
        noun = "tree";
        break;
    case 19:
        noun = "door";
        break;
    case 20:
        noun = "radio";
        break;
    }

    return noun;
}

回答by Luchian Grigore

You need to

你需要

#include <string>

<iostream>declares cout, cin, not string.

<iostream>声明cout, cin, 不是string

回答by Ernest Friedman-Hill

Nouns.hdoesn't include <string>, but it needs to. You need to add

Nouns.h不包括<string>,但需要。你需要添加

#include <string>

at the top of that file, otherwise the compiler doesn't know what std::stringis when it is encountered for the first time.

在该文件的顶部,否则编译器不知道std::string第一次遇到它时是什么。

回答by Pablo Santa Cruz

You need to add:

您需要添加:

#include <string>

In your header file.

在你的头文件中。