#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
string st = "Hello world";
return 0;
}
and
#include <string>
int main ()
{
std::string st = "Hello world";
return 0;
}
我尝试在 netbeans 上使用 minGW 编译器编译此代码。构建成功后,会出现以下错误。
RUN FAILED(退出值-1,073,741,511,总时间:93ms)
但当不使用字符串时它会干净地工作。我想知道我在这里做错了什么。提前致谢。
使用c++字符串而不使用using namespace std
:
#include <string> //c++ string header
int main ()
{
std::string st = "Hello world";
return 0;
}
#include <string.h> http://www.cplusplus.com/reference/cstring/是旧的 C 风格字符串标头,很可能不是您想要在此处使用的。请参阅此问题了解更多详细信息: 和 之间的区别? https://stackoverflow.com/questions/9257665/difference-between-string-and-string-h
注意:如果您确实想要旧的 C 风格字符串,那么您确实应该使用#include <cstring>
因为这会将这些函数放入std
命名空间,并且不会造成任何命名空间污染,从而导致其他不良结果。
可能发生的情况是您使用了旧式字符串标头并且没有正确初始化这些字符串。旧的 C 风格字符串没有像下面这样定义的构造函数和运算符=std::string
class.
编辑:查看 Netbeans 论坛后,这是 Netbeans 的问题,而不是 C++ 问题。尝试将输出更改为 Netbeans 中的外部终端。或者直接从命令行运行程序。如果这些方法不能解决问题或不受欢迎,请在 Netbeans 论坛上发帖。另外看看这个问题:程序不会在NetBeans中运行,而是在命令行上运行! https://stackoverflow.com/questions/3411386/program-wont-run-in-netbeans-but-runs-on-the-command-line
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)