c++ - 命名空间中的 "string is not a type"

标签 c++ string namespaces

我用两个文件用 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.hppstring 的命名空间。 stringstd 命名空间,但您并没有告诉编译器这一点。您可以通过将 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/

相关文章:

python 3删除字符串之前和之后

perl - 为什么 "use overload"不能与 "use namespace:autoclean"一起使用?

loops - xslt 循环属性因命名空间属性而失败

c++ - CL.exe 退出代码

c++ - 在 C++ 中清除 L1、L2 和 L3 缓存的最正确方法是什么?

c++ - CFile读取数据到缓冲区

c# - 在 C# 中查看一个字符串是否包含另一个字符串的最快、不区分大小写的方法是什么?

类中的 Java 字符串数组 - Eclipse 中的大括号错误

c++ - 有一个与命名空间同名的变量是否正确

c++ - 节点没有命名类型错误