当前位置:   article > 正文

error: ‘string’ does not name a type; did you mean ‘stdin’

error: ‘string’ does not name a type; did you mean ‘stdin’

今天写C++ 的时候遇到了这个错误

error: ‘string’ does not name a type; did you mean ‘stdin’
  • 1

代码如下

#ifndef EMP_H
#define EMP_H
#include <string>
#include <time.h>

class Person{
public:
	string name;
	int age;
	time_t birthday;
	Person(string na,int ag,time_t bir):name(na),age(ag){
		birthday = bir;
	}
};

#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

看了好一会儿,原来是string是标准库中的,而标准库中的需要加上命名空间std,所以改成

#ifndef EMP_H
#define EMP_H
#include <string>
#include <time.h>

class Person{
public:
	std::string name;
	int age;
	time_t birthday;
	Person(std::string na,int ag,time_t bir):name(na),age(ag){
		birthday = bir;
	}
};

#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

就可以了,如果我们改成#include <string.h>就不需要加std了,一般.h的头文件是为了兼容C代码,如果使用了标准库一定要加上命名空间。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/966568
推荐阅读
相关标签
  

闽ICP备14008679号