c++ - 为什么我得到字符串没有命名类型错误?

标签 c++ string std

游戏.cpp

#include <iostream>
#include <string>
#include <sstream>
#include "game.h"
#include "board.h"
#include "piece.h"

using namespace std;

游戏.h

#ifndef GAME_H
#define GAME_H
#include <string>

class Game
{
    private:
        string white;
        string black;
        string title;
    public:
        Game(istream&, ostream&);
        void display(colour, short);
};

#endif

错误是:

game.h:8 error: 'string' does not name a type
game.h:9 error: 'string' does not name a type

最佳答案

您的 using 声明位于 game.cpp 中,而不是您实际声明字符串变量的 game.h 中。您打算将 using namespace std; 放入标题中,在使用 string 的行上方,这将使这些行找到定义的 string 类型在 std 命名空间中。

作为 others have pointed out ,这是 not good practice在 header 中——每个包含该 header 的人也会不自觉地点击 using 行并将 std 导入他们的命名空间;正确的解决方案是将这些行改为使用 std::string

关于c++ - 为什么我得到字符串没有命名类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5527665/

相关文章:

c++ - 开关状态案例中的不同类型分配,内部模板函数

string - 在myBatis动态SQL中使用枚举参数

string - golang 中的 []string 和 ...string 有什么区别?

c# - 如何将字符串转换为十进制表达式?

c++ - 标准字符串查找方法和计数算法的逻辑错误

c++ - 如何在不包含 "using namespace std"的情况下在代码中使用 std?

C++ 指向一个对象然后移动它的内存位置

c++ - 可以将Boost消息队列文件重定向到用户指定的位置

c++ - C++ 中的 istream::unget() 没有像我想的那样工作

c++ - 应该使用我的 `swap` 重载吗?这是 libstdc++ (GCC) 错误吗?