初始化字符串流时出错
我不明白为什么我在“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不清楚该消息是如何生成的,但
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 astringstream
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, usestring
. If the purpose is to treat it as a stream, useistream &
orostream &
oriostream &
. You can still pass the samestringstream
because of polymorphism.I'm not really sure what you're doing here, but changing
stringstream
toiostream &
should fix the immediate problem and possible later issues too.您无法复制
stringstream
,请尝试通过引用传递它:You cannot copy
stringstream
, try to pass it by reference: