初始化字符串流时出错

发布于 2024-10-31 03:03:37 字数 872 浏览 1 评论 0原文

我不明白为什么我在“if (sentToGroup(client_fd, ss) == -1){”行收到此错误消息 运行以下代码时:

   stringstream ss;
    // ss.get();
    ss << "test";
    if (sentToGroup(client_fd, ss) == -1){
        perror("Fail sending to group");
    }

我收到以下错误消息,为什么?

Initializing argument 2 of ‘int sentToGroup(int, std::stringstream)’

sendToGroup函数如下:

int sentToGroup(int sender_fd, stringstream str){
    char buffer[MAX];
    stringstream sender;
    sender << int(sender_fd) << "> " << str;
    int bytes = recv(sender_fd, buffer, sizeof(buffer), 0);
    for (int c = printerCnt; c < sizeof(printer); c++){
        if (printer[c] != sender_fd){
            if (send(printer[c], sender, bytes, 0) == -1){
                return -1;
            }
        }
    }
    return 0;
}

I don't understand why I got this error message at line "if (sentToGroup(client_fd, ss) == -1){"
While running the following code:

   stringstream ss;
    // ss.get();
    ss << "test";
    if (sentToGroup(client_fd, ss) == -1){
        perror("Fail sending to group");
    }

I got the error message below, why??

Initializing argument 2 of ‘int sentToGroup(int, std::stringstream)’

The sentToGroup function is as below:

int sentToGroup(int sender_fd, stringstream str){
    char buffer[MAX];
    stringstream sender;
    sender << int(sender_fd) << "> " << str;
    int bytes = recv(sender_fd, buffer, sizeof(buffer), 0);
    for (int c = printerCnt; c < sizeof(printer); c++){
        if (printer[c] != sender_fd){
            if (send(printer[c], sender, bytes, 0) == -1){
                return -1;
            }
        }
    }
    return 0;
}

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

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

发布评论

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

评论(2

丶情人眼里出诗心の 2024-11-07 03:03:37

目前尚不清楚该消息是如何生成的,但 stringstream 不可复制。如果您不想传递 stringstream 但不修改它,则应该通过引用传递并从中复制数据。

但是,您通常根本不应该传递 stringstream。如果目的是将字符串传递给函数,请使用string。如果目的是将其视为流,请使用 istream & 或 ostream & 或 iostream & 。由于多态性,您仍然可以传递相同的stringstream

我不太确定您在这里做什么,但是将 stringstream 更改为 iostream & 应该可以解决当前的问题以及以后可能出现的问题。

It's not clear how that message is produced, but stringstream is not copyable. You should pass by reference and copy data out of it, if you do not wish to pass a stringstream but not modify it.

However, you typically should not pass a stringstream at all. If the purpose is to pass a string into the function, use string. If the purpose is to treat it as a stream, use istream & or ostream & or iostream &. You can still pass the same stringstream because of polymorphism.

I'm not really sure what you're doing here, but changing stringstream to iostream & should fix the immediate problem and possible later issues too.

土豪我们做朋友吧 2024-11-07 03:03:37

您无法复制stringstream,请尝试通过引用传递它:

int sentToGroup(int sender_fd, stringstream& str)

You cannot copy stringstream, try to pass it by reference:

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