命名空间std中的字符串不命名类型

40 c++ string namespaces std

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

我现在收到4个错误:

C:\ Documents and Settings\Me\My Documents\C++ Projects\C++\RandomSentence\Nouns.h | 8 | error:名称空间'std'中的'string'未命名类型

C:\ Documents and Settings\Me\My Documents\C++ Projects\C++\RandomSentence\Nouns.h | 12 | error:名称空间'std'中的'string'未命名类型

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

C:\ Documents and Settings\Me\My Documents\C++ Projects\C++\RandomSentence\Nouns.cpp | 9 | error:no'std :: string Nouns :: nounGenerator()'成员函数在类'Nouns'中声明|

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

这是我的头文件:

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()
};
Run Code Online (Sandbox Code Playgroud)

这是我的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;
}
Run Code Online (Sandbox Code Playgroud)

Luc*_*ore 73

你需要

#include <string>
Run Code Online (Sandbox Code Playgroud)

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

  • `<iostream>`可能间接声明`string`(在大多数版本中,无论如何)但请注意,在*`Nouns.h`已经处理之后,他将它包括在内.虽然这不是最好的解决方案,但颠倒两个包含的顺序也可能解决问题.当然,这会在其他地方再次出现`Nouns.h`,所以最好在该文件中包含`<string>`. (2认同)

Ern*_*ill 8

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

#include <string>
Run Code Online (Sandbox Code Playgroud)

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