当使用字符串时,c++程序给出一个运行时错误
C++ program gives a run-time error when strings are used
#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
string st = "Hello world";
return 0;
}
和
#include <string>
int main ()
{
std::string st = "Hello world";
return 0;
}
我尝试在netbeans上使用minGW编译器编译此代码。在成功构建之后,它会显示以下错误:
RUN FAILED (exit value -1,073,741,511, total time: 93ms)
但是当不使用字符串时,它可以干净地工作。我想知道我哪里做错了。
使用c++字符串,不要使用using namespace std
:
#include <string> //c++ string header
int main ()
{
std::string st = "Hello world";
return 0;
}
#include <string.h>
是旧的c风格字符串头,很可能不是你想在这里使用的。查看这个问题了解更多细节:
注意:如果你真的想要旧的c风格字符串,那么你真的应该使用#include <cstring>
,因为这将把这些函数放在std
命名空间中,并且不会造成任何命名空间污染,从而导致其他不希望的结果。
可能发生的事情是你使用了旧风格的字符串头,没有正确初始化这些字符串。旧的c风格字符串没有像std::string
类那样定义构造函数和操作符=。
us_ #include <string>
代替string.h
相关文章:
- 删除指向指针的指针是运行时错误吗
- c++中的指针和运行时错误
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 对单向链表进行排序时出现运行时错误
- 为什么此代码存在运行时错误?
- 你能解释一下什么运行时错误是如何解决它的吗?
- 字符* 复制到另一个字符* 运行时错误
- 我在代码中收到一个运行时错误,该错误如何通过修改最多一个元素来查找数组是否可以变得不减少
- C 运行时错误:Free():下一个大小无效(快速)
- 使用 memcpy 将动态数组的内容复制到另一个动态数组上会产生运行时错误
- 我想在我的代码中实现一个双重循环链表 - 运行时错误
- 我在这里运行时遇到一个奇怪的错误
- 我需要写一个函数,将十六进制转换为十进制,并使用递归,我不断得到运行时错误
- 我的c++程序读取一个矩阵并打印出非零的数字,这产生了一个运行时错误
- 当使用字符串时,c++程序给出一个运行时错误
- Linux中的c++运行时错误::glibc检测到free():无效的下一个大小(快速):堆栈具有void ptr元素
- 一个简单代码的运行时错误
- 两个几乎相同的C++程序,一个运行良好,但另一个出现运行时错误
- 向量迭代器擦除给我一个运行时错误