Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;-_不需要GNU_源? 首先,我对C++很陌生。我相信getline()不是一个标准的C函数,所以使用它需要\define\GNU\SOURCE。我现在使用C++和g++告诉我, .GuuuSoogle已经定义: $ g++ -Wall -Werror parser.cpp parser.cpp:1:1: error: "_GNU_SOURCE" redefined <command-line>: error: this is the location of the previous definition $g++-Wall-Werror parser.cpp parser.cpp:1:1:错误:“\u GNU\u SOURCE”已重新定义 :错误:这是上一个定义的位置_C++_Getline - Fatal编程技术网

C++;-_不需要GNU_源? 首先,我对C++很陌生。我相信getline()不是一个标准的C函数,所以使用它需要\define\GNU\SOURCE。我现在使用C++和g++告诉我, .GuuuSoogle已经定义: $ g++ -Wall -Werror parser.cpp parser.cpp:1:1: error: "_GNU_SOURCE" redefined <command-line>: error: this is the location of the previous definition $g++-Wall-Werror parser.cpp parser.cpp:1:1:错误:“\u GNU\u SOURCE”已重新定义 :错误:这是上一个定义的位置

C++;-_不需要GNU_源? 首先,我对C++很陌生。我相信getline()不是一个标准的C函数,所以使用它需要\define\GNU\SOURCE。我现在使用C++和g++告诉我, .GuuuSoogle已经定义: $ g++ -Wall -Werror parser.cpp parser.cpp:1:1: error: "_GNU_SOURCE" redefined <command-line>: error: this is the location of the previous definition $g++-Wall-Werror parser.cpp parser.cpp:1:1:错误:“\u GNU\u SOURCE”已重新定义 :错误:这是上一个定义的位置,c++,getline,C++,Getline,有人能确认这是标准的,还是它的定义隐藏在我的设置中?我不确定引用的最后一行的意思 文件的include如下所示,那么它大概是在其中一个或多个文件中定义的 #include <iostream> #include <string> #include <cctype> #include <cstdlib> #include <list> #include <sstream> #包括 #包括 #包括 #包括 #包括 #包括 谢

有人能确认这是标准的,还是它的定义隐藏在我的设置中?我不确定引用的最后一行的意思

文件的include如下所示,那么它大概是在其中一个或多个文件中定义的

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

谢谢

>我一直不得不使用C++中的一个。以前从未申报过任何东西。我通常在*nix中运行,所以我通常也使用gcc和g++

string s = cin.getline()

char c;
cin.getchar(&c);

我认为第3版的g++自动定义了
\u GNU\u SOURCE
。错误中的第三行支持这一点,指出第一个定义是在命令行上完成的(不存在
-D_GNU_SOURCE
):

您得到错误的原因是因为您正在定义它。如果您将它定义为已经存在的内容,那么它不应该是一个错误。至少C是这样的,它可能与C++不同。基于GNU头,我想说他们在做一个隐式的
-D_GNU_SOURCE=1
,这就是为什么它认为你在定义其他东西

如果您没有更改,下面的代码段应该会告诉您它的值

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

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');
供进一步参考

他的意思是:谢谢你的回复,帮我把事情弄清楚了。出于兼容性考虑,我将使用建议的预处理器。结尾的两行代码段不太正确:您需要额外的宏扩展级别。三行代码片段
(1)#定义STRINGIZE(x)#x(2)#定义DBG(x)printf(#x”=[“STRINGIZE(x)”]\n”)(3)DBG(#GNU#SOURCE)
给了我:
\u GNU_SOURCE=[1]
,更一般地说,
DBG(x)
也许应该定义为
printf(“%s\n”…..)
,以处理
\x
包含
%
的远程可能性。
//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');