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