'string' : undeclared identifier

string' : undeclared identifier

 

错误:

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

……

string    st("test    string");  

……

error    C2065:    'string'    :    undeclared    identifier  

解释:

#include    <string>   

using    namespace    std;   

因为在    using    namespace    std;    情况下,

#include    <string>      是使用    C++      string    类库;

#include    <cstring>      是使用    C    的string    操作库函数    ...  

细节 加不加“.h”

#include <iostream.h>

#include <string>

using        namespace       std;

没有错!!

#include <iostream.h>

#include <string.h>

using        namespace       std;

编译有错!!

解释

   “string.h“这个头文件是“旧式c头文件”,而这个文件中没有定义string类(这点应该不奇怪,c语言中哪有什么类啊),这个头文件里面是有 关“旧式char-based字符串”的操作函数,注意都是操作char*字符串的“函数”,所以你引用这个头文件,编译器肯定找不到“string” 了。   
   “string”这个头文件(没有扩展名)是C++标准化之后的C++头文件,里面才有string类的相关定义(其实,string并不是类,是一个 typedef,但是使用的时候不用去管他),而C++标准头文件中的东西都放在namespace    std中了,所以使用的时候要“using    namespace    std”。   
   附:建议楼主不要用"iostream.h",改成“iostream”吧,因为标准已经明确规定不在支持"iostream.h"这种头文件了。

标准写法

#include <iostream>

#include <string>

using        namespace       std;

F: 为什么using    namespace    std;   
        要写在include后面?

Q: 因为include的文件包含名字域std   
如果你把using    namespace    std写在前面,编译器就看不到std这个名字

转载于:https://www.cnblogs.com/cree/p/3177365.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值