为什么我得到字符串不命名类型错误? [英] Why am I getting string does not name a type Error?

查看:129
本文介绍了为什么我得到字符串不命名类型错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

game.cpp



  #include< iostream& 
#include< string>
#include< sstream>
#includegame.h
#includeboard.h
#includepiece.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; amp;);
void display(color,short);
};

#endif






错误是:


game.h:8错误:'string'未命名类型

game.h:9错误:'string'未命名类型



解决方案

您的使用声明在 game.cpp ,而不是 game.h 你实际上声明字符串变量。你打算把使用命名空间std; 插入到使用 string 的行上面的标题中,找到 std 命名空间中定义的 string 类型。



由于其他人指出 ,这是不是良好做法包含该标题的每个人也将不由自主地击中使用行并将 std 导入到其命名空间;正确的解决方案是改变这些行使用 std :: string 而不是


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

解决方案

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.

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

这篇关于为什么我得到字符串不命名类型错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆