我用两个文件用 c++ 编写了一个程序。
main.cpp
#include "var.hpp"
#include <iostream>
using namespace std;
using namespace YU;
int main()
{
string god = "THL";
age = 10;
cout << age << endl;
cout << god << endl;
return 0;
}
var.hpp
#ifndef __VAR_H__
#define __VAR_H__
#include <string>
namespace YU
{
int age;
string name;
}
#endif
编译的时候出错了
错误的信息是:
In file included from main.cpp:1:0:
var.hpp:9:5: Error: ‘string’ is not a type name
我不知道为什么,我有 include <string>
头文件,但它仍然不起作用。
我写这段代码只是为了练习,不是为了工作。
谢谢!
最佳答案
问题出在 var.hpp
中 string
的命名空间。 string
是 std
命名空间,但您并没有告诉编译器这一点。您可以通过将 using namespace std;
放在 var.hpp
中来修复它,但以下是更好的解决方案,因为它不会将全局 namespace 与来自 标准
:
#ifndef __VAR_H__
#define __VAR_H__
#include <string>
namespace YU
{
int age;
std::string name;
}
#endif
关于c++ - 命名空间中的 "string is not a type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10256106/