首先,我很新的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版,自动地限定_GNU_SOURCE

:这是通过在错误的第三行,指出第一个定义在命令行上进行(在视线进制-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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top