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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5527665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:22:00  来源:igfitidea点击:

Why am I getting string does not name a type Error?

c++stringstd

提问by Steven

game.cpp

游戏.cpp

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

using namespace std;

game.h

游戏.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


The error is:

错误是:

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

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

回答by Michael Mrozek

Your usingdeclaration is in game.cpp, not game.hwhere you actually declare string variables. You intended to put using namespace std;into the header, above the lines that use string, which would let those lines find the stringtype defined in the stdnamespace.

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

As others have pointed out, this is not good practicein headers -- everyone who includes that header will also involuntarily hit the usingline and import stdinto their namespace; the right solution is to change those lines to use std::stringinstead

正如其他人指出的那样,这在标头中并不是一个好习惯——每个包含该标头的人也会不由自主地点击该using行并导入std到他们的命名空间中;正确的解决办法是改变那些线使用std::string,而不是

回答by Johnsyweb

stringdoes not name a type. The class in the stringheader is called std::string.

string不命名类型。string标题中的类称为std::string

Please do notput using namespace stdin a header file, it pollutes the global namespace for all users of that header. See also "Why is 'using namespace std;' considered a bad practice in C++?"

不要放入using namespace std头文件,它会污染该头文件的所有用户的全局命名空间。另见“为什么是‘使用命名空间标准;’ 在 C++ 中被认为是一种不好的做法?”

Your class should look like this:

你的类应该是这样的:

#include <string>

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

回答by quamrana

Just use the std::qualifier in front of stringin your header files.

只需在头文件中使用std::前面的限定符string

In fact, you should use it for istreamand ostreamalso - and then you will need #include <iostream>at the top of your header file to make it more self contained.

事实上,您应该将它用于istreamostream- 然后您将需要#include <iostream>在头文件的顶部使其更加独立。

回答by Borealid

Try a using namespace std;at the top of game.hor use the fully-qualified std::stringinstead of string.

using namespace std;在顶部尝试 agame.h或使用完全限定的std::string而不是string

The namespacein game.cppis after the header is included.

namespacegame.cpp是被包括在报头之后。