无法将 cout 与 C++ 一起使用字符串,除非我通过 data() 或 c_str() 运行它
在下面的程序中,当我尝试使用 cout
将 C++ 字符串输出到 stdout
时,我得到了这个结果 - 其他指令产生预期的输出。我在 Windows 7 系统上使用 MS Visual Studio 2010。
Lab1.exe 中 0x00dd4e89 处的第一次机会异常:0xC00000FD:堆栈 溢出。 Lab1.exe 中 0x00dd4e89 处未处理的异常:0xC00000FD: 堆栈溢出。程序“[3740] Lab1.exe:Native”已退出 代码-1073741571(0xc00000fd)。
#include "StdAfx.h"
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <ostream>
#include <string>
#include <ctime>
//more code here
int main() {
int number = 1;
string myStr = "Hello, string!";
cout << "number: " << number << endl;
cout << "Hello, World!" << endl;
cout << myStr << endl; //failing instruction
cout << "\nHit any key to continue...." << endl;
cin.get();
return 0;
}
我的导师建议将失败的指令更改为使用 data()
或 c_str()
,如下所示:
cout << myStr.data() << endl;
我这样做了,这解决了问题。他不知道为什么,只是说这有效,所以不用担心。
在我看来,像 cout 这样的 C++ ostream 对象应该能够处理 C++ 字符串。我是否遗漏了某些内容,或者我真的需要将 data()
或 c_str()
与 cout
一起使用?
我还尝试使用 std::cout
、std::string
和 std::endl
- 但没有帮助。
预先感谢您的建议;我真的很想了解这里发生了什么。
海伦
On the following program, I'm getting this when I attempt to use cout
to output a C++ string to stdout
- the other instructions produce the expected output. I'm using MS Visual Studio 2010 on a Windows 7 system.
First-chance exception at 0x00dd4e89 in Lab1.exe: 0xC00000FD: Stack
overflow. Unhandled exception at 0x00dd4e89 in Lab1.exe: 0xC00000FD:
Stack overflow. The program '[3740] Lab1.exe: Native' has exited with
code -1073741571 (0xc00000fd).
#include "StdAfx.h"
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <ostream>
#include <string>
#include <ctime>
//more code here
int main() {
int number = 1;
string myStr = "Hello, string!";
cout << "number: " << number << endl;
cout << "Hello, World!" << endl;
cout << myStr << endl; //failing instruction
cout << "\nHit any key to continue...." << endl;
cin.get();
return 0;
}
My instructor suggested changing the failing instruction to use data()
or c_str()
like so:
cout << myStr.data() << endl;
I did this, and this resolved the problem. He didn't know why, just said it worked so not to worry about it.
It seems to me that a C++ ostream object like cout should be able to handle a C++ string. Am I missing something, or do I really need to use data()
or c_str()
with cout
?
I also tried using std::cout
, std::string
, and std::endl
- it didn't help.
Thanks in advance for your advice; I'm really wanting to understand what's going on here.
Helen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该包含
string
而不是string.h
:You should include
string
instead ofstring.h
:我怀疑
cout << myStr << endl;
是很麻烦的一行。这段代码工作正常:
I doubt that
cout << myStr << endl;
was the troublesome line.This code works fine:
该错误消息表明您有堆栈溢出:似乎正在递归调用某些函数。您没有为
string
定义自己的输出函数吗? “更多代码”中的内容可能与输出运算符相关?The error message indicates that you have a stack overflow: it seems some function is being called recursively. You didn't define your own output function for
string
by any chance? What is in "more code here" which may be related to output operators?