문자열에 오류 유형이 지정되지 않는 이유는 무엇입니까?
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
오류는 다음과 같습니다.
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
네임 스페이스에 정의 된 유형을 찾을 수 있습니다.
다른 사람들이 지적했듯이 이것은 헤더에서 좋은 관행 이 아닙니다 . 헤더를 포함하는 모든 사람은 또한 무의식적으로 using
행을 치고 std
네임 스페이스로 가져옵니다 . 올바른 해결책은 std::string
대신 사용할 라인을 변경하는 것입니다.
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
헤더가 포함 된 후입니다.
참고 URL : https://stackoverflow.com/questions/5527665/why-am-i-getting-string-does-not-name-a-type-error
'Nice programing' 카테고리의 다른 글
정규식을 사용하는 Scala 캡처 그룹 (0) | 2020.11.23 |
---|---|
li : before {content : "■"; (0) | 2020.11.23 |
배경 이미지 위에 CSS 그라데이션 사용 (0) | 2020.11.23 |
xcode 5.1의 Arm64 아키텍처 (0) | 2020.11.23 |
WebSockets ping / pong, TCP keepalive가 아닌 이유는 무엇입니까? (0) | 2020.11.23 |