无法将 cout 与 C++ 一起使用字符串,除非我通过 data() 或 c_str() 运行它

发布于 2025-01-01 08:40:18 字数 1342 浏览 4 评论 0原文

在下面的程序中,当我尝试使用 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::coutstd::stringstd::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七七 2025-01-08 08:40:18

您应该包含 string 而不是 string.h

#include <string>

You should include string instead of string.h:

#include <string>
⊕婉儿 2025-01-08 08:40:18

我怀疑 cout << myStr << endl; 是很麻烦的一行。

这段代码工作正常:

#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    string s("Hello World!");
    cout << s << endl;
    return 0;
}

I doubt that cout << myStr << endl; was the troublesome line.

This code works fine:

#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    string s("Hello World!");
    cout << s << endl;
    return 0;
}
梦屿孤独相伴 2025-01-08 08:40:18

该错误消息表明您有堆栈溢出:似乎正在递归调用某些函数。您没有为 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文