#include <iostream>
#include <string>
#include <sstream>
#include "game.h"
#include "board.h"
#include "piece.h"
using namespace std;
#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
string
不命名类型。
string
标头中的类称为 std::string
。请不要
将using namespace std
放入头文件中,它会污染该头文件的所有用户的全局命名空间。另请参阅
“为什么是‘using namespace std;’在 C++ 中被认为是不好的做法吗?”你的课程应该是这样的:
#include <string>
class Game
{
private:
std::string white;
std::string black;
std::string title;
public:
Game(std::istream&, std::ostream&);
void display(colour, short);
};
std::
前面使用
string
限定符即可。事实上,您还应该将它用于
istream
和
ostream
- 然后您将需要在头文件顶部使用 #include <iostream>
以使其更加独立。using namespace std;
顶部添加
game.h
或使用完全限定的 std::string
代替 string
。namespace
中的
game.cpp
位于包含标题之后。第一种方法
using namespace std;
include <string>
// then you can use string class the normal way
// after including the class string in your cpp file as follows
include <string>
/*Now when you are using a string class you have to put **std::** before you write
string as follows*/
std::string name; // a string declaration