为什么我收到 string does not name a type 错误?

2024-05-07

game.cpp

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

using namespace std;

game.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


Your using声明位于game.cpp, not game.h您实际声明字符串变量的地方。你打算把using namespace std;进入标题,在使用的行上方string,这会让这些行找到string中定义的类型std命名空间。

As 其他人指出 https://stackoverflow.com/questions/5527665/weird-string-does-not-name-a-type-error-c/5527754#5527754, 这是不是好的做法 https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c在标题中——每个包含该标题的人也会不自觉地点击using线路和进口std进入他们的命名空间;正确的解决方案是更改这些行以使用std::string instead

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我收到 string does not name a type 错误? 的相关文章

随机推荐