命名空间 std 中的字符串不命名类型
string in namespace std does not name a type
这可能只是一个我没有看到的简单错误,但是我想我只是做错了什么。别担心我没有在我的标头中使用命名空间 std 函数或任何似乎是这个人的问题[我读过类似的问题我的][1] [1]:为什么我得到字符串不命名类型错误?
我现在收到 4 个错误:
C:\文档和设置\我\我的 文档\C++项目\C++\随机句子\名词.h|8|错误:"字符串" 命名空间 'std' 不命名类型|
C:\文档和设置\我\我的 文档\C++项目\C++\随机句子\名词.h|12|错误: 'string' in 命名空间 'std' 不命名类型|
C:\文档和设置\我\我的 文档\C++项目\C++\随机句子\名词.h|13|错误: 'string' in 命名空间 'std' 不命名类型|
C:\文档和设置\我\我的 文档\C++项目\C++\随机句子\名词.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;
}
你需要
#include <string>
<iostream>
声明的是cout
,cin
,而不是string
。
Nouns.h
不包括<string>
,但它需要。您需要添加
#include <string>
在该文件的顶部,否则编译器在第一次遇到std::string
时不知道它是什么。
您需要添加:
#include <string>
在您的头文件中。
注意
#include <string.h>
莫
#include <string>
你需要添加
#include <string>
在这里,您尝试访问string noun::
但没有创建名为string noun
的命名空间。您正在尝试访问私人文件。
相关文章:
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 类型字符串::长度()。我得到了"Unable to resolve identifier length"
- 如何掩盖对象数组的数据类型字符串到C 中的INT
- 解析参数值 - 字符串和链接值(类型字符串的链接值)与(特定类型的)变量
- C 中的枚举类型字符串类别
- 与基本面作斗争.特别是字符[],字符*和从数组中读取.也许我应该使用类型字符串
- 矢量类型 字符串,但当用整数初始化时
- 如何从const char *类型字符串中删除新线字符
- 在 c++ 中将 ostream 分配给数据类型字符串
- Linux 编译错误 - 无法将类型"字符串"转换为类型"字符串&&"
- LPCSTR 类型字符串出现乱码或显示错误
- C 表达式必须具有类型 - 字符串到const char*
- 将值分配给字符类型字符串
- 将组合框中的类型字符串与其项目进行比较
- 如何将返回类型字符串的数据复制到C++中
- 如何处理csv文件输入流,我需要将文件的每一行中的数据划分为6个不同的变量(类型字符串和int)
- 自定义C类型字符串类的问题
- 在c++中创建自定义C类型字符串类
- 在读取.csv时将数组类型字符串转换为double
- C++将类型字符串变量中的单词转换为小写