今天早些时候(由于我的时区实际上是昨天)我在Interview Street上,使用Visual Studio 2012进行C++编程面试(该平台使用g++)。
简单来说,在我的编程过程中遇到了几个编译错误1。
#include <cstring>
这段代码是从一个问题的骨架代码中提供的,经过修改后变成了
#include <string>
所有编译错误都神奇地消失了。
然而,当我提交到Interview Street时,我不得不再添加c
;否则我就会得到编译错误。
这是我第一次被非标准化所困扰....
我的问题是:在<string>
和<cstring>
中有什么让我(宝贵的)超过半个小时?
1 对于任何好奇的人:
如果using <cstring>
,Visual Studio 2012的一个错误是:
error C2338:C++标准不为此类型提供散列。
出现在
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef
可能是unordered_map
中以string
为键值。
如果using <string>
,g ++的一个错误是:
'strlen' was not declared in this scope
<string>
来使用string
,而 Visual Studio 则不需要<cstring>
来使用 c 的东西。为什么? - Dante May Codeiostream
)中获取string
的内容。cstring
同理。请始终包含所有所需的头文件。不要依赖于特定环境隐式地为您包含某些头文件;如果需要使您的代码可移植,或者如果更改编译器版本并且新版具有不同的隐式头文件依赖项,则会遇到问题。 - Rob Kennedy#include <cstring>
,而gcc会在您所包含的某个头文件中添加#include <string>
。您应该同时包含这两个头文件,因为您的代码依赖于它们。 - David Rodríguez - dribeas