Getline () in C ++ - _GNU_SOURCE not needed?

First off, I'm pretty new to C ++. I believe that getline()this is not a standard C function, so it is required to use it #define _GNU_SOURCE. Now I use C ++ and g ++ tells me what is _GNU_SOURCEalready defined:

$ g++ -Wall -Werror parser.cpp
parser.cpp:1:1: error: "_GNU_SOURCE" redefined
<command-line>: error: this is the location of the previous definition

Can anyone confirm if this is standard, or is its definition hidden somewhere in my setup? I am not sure about the meaning of the last line.

The file includes the following, so is it supposedly defined in one or more of them?

#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <list>
#include <sstream>

Thanks!

+3
source share
3 answers

, g++, 3, _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 ++. GNU, , -D_GNU_SOURCE=1, , - .

, .

#define DBG(x) printf ("_GNU_SOURCE = [" #x "]\n")
DBG(_GNU_SOURCE); // first line in main.
+5

++. _GNU_ . * nix, gcc g++.

string s = cin.getline()

char c;
cin.getchar(&c);
0

Getline is standard; it is defined in two ways.
You can name it as a member function of one of the threads as follows: This is the version defined in

//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');

or you can use the version defined in

//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');

for further links http://www.cplusplus.com/reference/iostream/istream/getline.html http://www.cplusplus.com/reference/string/getline.html

0
source

Source: https://habr.com/ru/post/1703398/


All Articles