为什么字符串没有命名类型错误?

时间:2022-11-25 16:35:38

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

The error is:

错误的是:

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

游戏。h:8错误:'string'没有命名类型游戏。h:9个错误:“字符串”不指定类型。

4 个解决方案

#1


79  

Your using declaration is in game.cpp, not game.h where 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 string type defined in the std namespace.

您的使用声明在游戏中。cpp,不是游戏。h表示字符串变量。您打算使用名称空间std放置;在头中,在使用字符串的行之上,这将使这些行找到在std名称空间中定义的字符串类型。

As others have pointed out, this is not good practice in headers -- everyone who includes that header will also involuntarily hit the using line and import std into their namespace; the right solution is to change those lines to use std::string instead

正如其他人所指出的,这在header中不是很好的实践——每个包含该header的人都会不由自主地撞到using行并将std导入到他们的名称空间中;正确的解决方案是将这些行改为使用std::string

#2


33  

string does not name a type. The class in the string header is called std::string.

字符串不命名类型。字符串头中的类称为std::string。

Please do not put using namespace std in 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++?"

请不要在头文件中使用名称空间std,它会为该标题的所有用户污染全局名称空间。请参见“为什么使用名称空间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);
};

#3


6  

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

只需在头文件的字符串前面使用std:: qualifier即可。

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

实际上,您应该将它用于istream和ostream——然后您需要在头文件的顶部使用#include ,以使其更自包含。

#4


3  

Try a using namespace std; at the top of game.h or use the fully-qualified std::string instead of string.

尝试使用名称空间std;在游戏的顶端。h或使用完全限定的std::string而不是string。

The namespace in game.cpp is after the header is included.

游戏的名称空间。cpp在包含报头之后。

#1


79  

Your using declaration is in game.cpp, not game.h where 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 string type defined in the std namespace.

您的使用声明在游戏中。cpp,不是游戏。h表示字符串变量。您打算使用名称空间std放置;在头中,在使用字符串的行之上,这将使这些行找到在std名称空间中定义的字符串类型。

As others have pointed out, this is not good practice in headers -- everyone who includes that header will also involuntarily hit the using line and import std into their namespace; the right solution is to change those lines to use std::string instead

正如其他人所指出的,这在header中不是很好的实践——每个包含该header的人都会不由自主地撞到using行并将std导入到他们的名称空间中;正确的解决方案是将这些行改为使用std::string

#2


33  

string does not name a type. The class in the string header is called std::string.

字符串不命名类型。字符串头中的类称为std::string。

Please do not put using namespace std in 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++?"

请不要在头文件中使用名称空间std,它会为该标题的所有用户污染全局名称空间。请参见“为什么使用名称空间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);
};

#3


6  

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

只需在头文件的字符串前面使用std:: qualifier即可。

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

实际上,您应该将它用于istream和ostream——然后您需要在头文件的顶部使用#include ,以使其更自包含。

#4


3  

Try a using namespace std; at the top of game.h or use the fully-qualified std::string instead of string.

尝试使用名称空间std;在游戏的顶端。h或使用完全限定的std::string而不是string。

The namespace in game.cpp is after the header is included.

游戏的名称空间。cpp在包含报头之后。