Nice programing

문자열에 오류 유형이 지정되지 않는 이유는 무엇입니까?

nicepro 2020. 11. 23. 20:01
반응형

문자열에 오류 유형이 지정되지 않는 이유는 무엇입니까?


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했는데, 이렇게하면 해당 줄 stringstd네임 스페이스에 정의 된 유형을 찾을 수 있습니다.

다른 사람들이 지적했듯이 이것은 헤더에서 좋은 관행아닙니다 . 헤더를 포함하는 모든 사람은 또한 무의식적으로 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

반응형