txs
txs

Reputation: 143

error in initializing stringstream

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;
}

Upvotes: 0

Views: 1381

Answers (2)

Potatoswatter
Potatoswatter

Reputation: 137780

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.

Upvotes: 4

Kirill V. Lyadvinsky
Kirill V. Lyadvinsky

Reputation: 99565

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

int sentToGroup(int sender_fd, stringstream& str)

Upvotes: 1

Related Questions