c ++ - _gnu_source의 getline ()가 필요하지 않습니까?
문제
첫째, 나는 C ++에 익숙하지 않습니다. 나는 그것을 믿는다 getline()
표준 C 기능이 아닙니다 #define _GNU_SOURCE
그것을 사용해야합니다. 나는 지금 C ++를 사용하고 있고 g ++를 사용하고 있습니다. _GNU_SOURCE
이미 정의되어 있습니다.
$ g++ -Wall -Werror parser.cpp
parser.cpp:1:1: error: "_GNU_SOURCE" redefined
<command-line>: error: this is the location of the previous definition
누구든지 이것이 표준인지 확인할 수 있습니까? 아니면 내 설정의 어딘가에 정의가 숨겨져 있습니까? 나는 최종 줄의 의미를 확신하지 못한다.
파일 포함은 다음과 같습니다.
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <list>
#include <sstream>
감사!
해결책
버전 3에서 G ++가 자동으로 정의한다고 생각합니다. _GNU_SOURCE
. 이것은 첫 번째 정의가 명령 줄에서 수행되었다는 오류에서 세 번째 줄에 의해 뒷받침됩니다 (nary a와 함께 -D_GNU_SOURCE
통찰력):
<command-line>: error: this is the location of the previous definition
원하지 않는다면 #undef
컴파일 장치의 첫 번째 줄입니다. 그러나 필요한 경우 사용이 필요할 수 있습니다.
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
당신이 오류를 얻는 이유는 당신이 그것을 다시 정의하기 때문입니다. 이미 있었던 것에 대해 정의하면 오류가되지 않아야합니다. 적어도 그것은 C의 경우입니다. C ++와 다를 수 있습니다. GNU 헤더를 기반으로, 나는 그들이 암시 적으로하고 있다고 말할 것입니다. -D_GNU_SOURCE=1
그것이 당신이 생각하는 이유입니다 다시 정의 그것은 다른 것입니다.
다음 스 니펫은 변경하지 않은 경우 그 가치를 알려야합니다.
#define DBG(x) printf ("_GNU_SOURCE = [" #x "]\n")
DBG(_GNU_SOURCE); // first line in main.
다른 팁
나는 항상 C ++에서 다음 중 하나를 사용해야했습니다. 전에 _gnu_를 선언 할 필요가 없었습니다. 나는 보통 *nix에서 실행되므로 보통 GCC와 G ++도 사용합니다.
string s = cin.getline()
char c;
cin.getchar(&c);
GetLine은 표준이며 두 가지 방식으로 정의됩니다.
다음과 같이 스트림 중 하나의 멤버 함수라고 부를 수 있습니다. 이것은 정의 된 버전입니다.
//the first parameter is the cstring to accept the data
//the second parameter is the maximum number of characters to read
//(including the terminating null character)
//the final parameter is an optional delimeter character that is by default '\n'
char buffer[100];
std::cin.getline(buffer, 100, '\n');
또는 정의 된 버전을 사용할 수 있습니다
//the first parameter is the stream to retrieve the data from
//the second parameter is the string to accept the data
//the third parameter is the delimeter character that is by default set to '\n'
std::string buffer;
std::getline(std::cin, buffer,'\n');
추가 참조http://www.cplusplus.com/reference/iostream/istream/getline.html http://www.cplusplus.com/reference/string/getline.html