这可能只是一个我没有看到的简单错误,但是 我想我只是做错了什么。别担心我在我的标题中没有使用命名空间std 功能或任何似乎是这个人的问题[问题我读类似于 我的[1] [1]:Why am I getting string does not name a type Error?
我现在收到4个错误:
C:\ Documents and Settings \ Me \ My Documents \ C ++ Projects \ C ++ \ RandomSentence \ Nouns.h | 8 | error:'string'in namespace'std'没有命名类型|
C:\ Documents and Settings \ Me \ My Documents \ C ++ Projects \ C ++ \ RandomSentence \ Nouns.h | 12 | error:'string'in namespace'std'没有命名类型|
C:\ Documents and Settings \ Me \ My Documents \ C ++ Projects \ C ++ \ RandomSentence \ Nouns.h | 13 | error:'string'in namespace'std'没有命名类型|
C:\ Documents and Settings \ Me \ My Documents \ C ++ Projects \ C ++ \ RandomSentence \ Nouns.cpp | 9 |错误:没有 'std :: string Nouns :: nounGenerator()'在类中声明的成员函数 '名词' |
|| ===构建完成: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()
};
这是我的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;
}
答案 0 :(得分:66)
你需要
#include <string>
<iostream>
声明cout
,cin
,而非string
。
答案 1 :(得分:8)
Nouns.h
不包含<string>
,但它需要。你需要添加
#include <string>
位于该文件的顶部,否则编译器在第一次遇到std::string
时就不知道它是什么。
答案 2 :(得分:4)
您需要添加:
#include <string>
在你的头文件中。
答案 3 :(得分:-1)
您需要添加
#include <string>
此处您尝试访问 string noun::
,但未创建名为 string noun
的命名空间。
您正在尝试访问私人文件。