sendMessages
takes a std::ofstream
by value, as std::ofstream
is not copyable, even without std::thread
you can only call it with a temporary or r value.
For example this won't compile:
MessageHandler::sendMessages(MenuVariables::messages, MenuVariables::usernames, outputFile);
but either of these will:
MessageHandler::sendMessages(MenuVariables::messages, MenuVariables::usernames, std::move(outputFile));
MessageHandler::sendMessages(MenuVariables::messages, MenuVariables::usernames, ofstream("output.txt"));
You therefore have the same options to make your std::thread
constructor work:
thread sendThread(MessageHandler::sendMessages, ref(MenuVariables::messages), ref(MenuVariables::usernames), std::move(outputFile));
thread sendThread(MessageHandler::sendMessages, ref(MenuVariables::messages), ref(MenuVariables::usernames), ofstream("output.txt"));
Alternatively change sendMessages
to take outputFile
by reference:
void sendMessages(queue<string>& messages, set<string>& users, ofstream& outputFile);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…