C++ 返回字符串的函数不起作用,除非涉及 endl...?

发布于 2024-07-13 02:26:18 字数 1465 浏览 3 评论 0 原文

我在类中有一个返回字符串的函数。 在这个函数中,只有在 return 语句之前将 cout< 添加到函数中时,我才能使其工作。 知道这是为什么吗,或者我该如何解决它? 我在 Mac 上的 Eclipse 中运行这个

In "main.cpp":

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include "Braid.h"
using namespace std;

static int size=3;

int main(){  
Braid * b1 = new Braid(size);
b1->setCanon();//creates canonical braid. 

cout<<"a ";
cout<<b1->getName()<<endl;
cout<<" b ";
}  

In "Braid.h" :

  public:  
        Braid(int);  
        void setCanon();  
        string getName(); 
    };  

And in "Braid.cpp":

string Braid::getName(){  
    string sName="";  

    /* body commented out
    for(int i=0; i<height; i++)
    {
        for(int j=2; j<(width-2); j++)
            {
                sName += boxes[i][j];
                sName += "|";
            }
        }

        */  
        //cout<<endl;
        return sName;
    } 

当我运行我的主代码时,没有注释该函数的主体,输出我得到的是
"a 0|0|12|12|0|0|2|1|1|1|1|2|"

它返回的“名称”是正确的,但它没有超过函数调用。 如果我取消注释 //cout< 行,该函数将起作用并且我的输出为
“0|0|12|12|0|0|2|1|1|1|1|2|
b "

注释掉函数体后,它只创建一个空字符串并返回它,我的输出只有“a”,然后如果我添加 endl 回来,我会得到“a b” 我做错了什么

吗?我缺少什么东西吗?

I've got a function inside of a class that returns a string. Inside this function, I can only get it to work when I add cout<<endl to the function before the return statement. Any idea why this is, or how I can fix it? I'm running this in Eclipse on a Mac

In "main.cpp":

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include "Braid.h"
using namespace std;

static int size=3;

int main(){  
Braid * b1 = new Braid(size);
b1->setCanon();//creates canonical braid. 

cout<<"a ";
cout<<b1->getName()<<endl;
cout<<" b ";
}  

In "Braid.h" :

  public:  
        Braid(int);  
        void setCanon();  
        string getName(); 
    };  

And in "Braid.cpp":

string Braid::getName(){  
    string sName="";  

    /* body commented out
    for(int i=0; i<height; i++)
    {
        for(int j=2; j<(width-2); j++)
            {
                sName += boxes[i][j];
                sName += "|";
            }
        }

        */  
        //cout<<endl;
        return sName;
    } 

When I run my main code, without the body of that function commented, the output I get is
"a 0|0|12|12|0|0|2|1|1|1|1|2|"

The "name" it returns is correct, but it's not making it past the function call. If I uncomment the //cout<<endl line, the function works and my output is
"a 0|0|12|12|0|0|2|1|1|1|1|2|
b "

After commenting out the body of the function, so that it only creates an empty string, and returns it, my output is only "a" then if I add the endl back, I get the "a b" that is expected.

What am I doing wrong? Is there something that comes with endl that I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

挽清梦 2024-07-20 02:26:18

实际上,getName() 函数可能工作正常。 但是,cout 会“缓存”输出(即,当内部文本缓冲区已满时,它会在屏幕上打印输出)。 'endl' 刷新缓冲区并强制 cout 将文本(在缓存中)转储到屏幕。

尝试 main.cpp 中的 cout.flush()

actually getName() function is probably working correctly. However, the cout 'caches' the output (i.e. it prints the output on screen when it's internal text buffer is full). 'endl' flushes the buffer and forces cout to dump the text (in cache) to screen.

Try cout.flush() in main.cpp

晚风撩人 2024-07-20 02:26:18

也许你的终端很懒。 尝试省略 endl 并插入 cout.flush() 作为下一行。

Maybe your terminal is lazy. Try omitting the endl and insert cout.flush() as the next line.

蓝天 2024-07-20 02:26:18

cout 应该在程序结束时刷新。

fow@lapbert ~ % cat blah.cpp
#include <iostream>

int main() {
    std::cout << sizeof(int);
}
fow@lapbert ~ % ./a.out
4

cout should be flushed at program end.

fow@lapbert ~ % cat blah.cpp
#include <iostream>

int main() {
    std::cout << sizeof(int);
}
fow@lapbert ~ % ./a.out
4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文