gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:20:46 28 4
gpt4 key购买 nike

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com