今天写C++ 的时候遇到了这个错误
error: ‘string’ does not name a type; did you mean ‘stdin’
代码如下
#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
看了好一会儿,原来是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
就可以了,如果我们改成#include <string.h>就不需要加std了,一般.h的头文件是为了兼容C代码,如果使用了标准库一定要加上命名空间。